const mongoose = require('mongoose');

const slaTargetSchema = new mongoose.Schema({
  priority: {
    type: String,
    enum: ['low', 'medium', 'high', 'urgent', 'critical'],
    required: true
  },
  responseTime: {
    value: {
      type: Number,
      required: true
    },
    unit: {
      type: String,
      enum: ['minutes', 'hours', 'days'],
      default: 'hours'
    }
  },
  resolutionTime: {
    value: {
      type: Number,
      required: true
    },
    unit: {
      type: String,
      enum: ['minutes', 'hours', 'days'],
      default: 'hours'
    }
  },
  escalationLevels: [{
    level: Number,
    triggerAfter: {
      value: Number,
      unit: {
        type: String,
        enum: ['minutes', 'hours', 'days']
      }
    },
    escalateTo: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    },
    notifyUsers: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    }]
  }]
}, { _id: false });

const businessHoursSchema = new mongoose.Schema({
  enabled: {
    type: Boolean,
    default: true
  },
  timezone: {
    type: String,
    default: 'Asia/Kolkata'
  },
  workingDays: [{
    type: String,
    enum: ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
  }],
  workingHours: {
    start: String, // Format: "09:00"
    end: String    // Format: "18:00"
  },
  holidays: [{
    date: Date,
    name: String
  }]
}, { _id: false });

const slaSchema = new mongoose.Schema({
  companyId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Company',
    required: true
  },
  name: {
    type: String,
    required: [true, 'SLA name is required'],
    trim: true
  },
  description: {
    type: String,
    trim: true
  },
  type: {
    type: String,
    enum: ['ticket', 'lead', 'proposal', 'task', 'general'],
    required: true
  },
  targets: [slaTargetSchema],
  businessHours: businessHoursSchema,
  notifications: {
    onBreach: {
      enabled: {
        type: Boolean,
        default: true
      },
      notifyUsers: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
      }],
      notifyRoles: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Role'
      }],
      emailEnabled: {
        type: Boolean,
        default: true
      },
      smsEnabled: {
        type: Boolean,
        default: false
      }
    },
    onWarning: {
      enabled: {
        type: Boolean,
        default: true
      },
      threshold: {
        type: Number,
        default: 80, // Percentage
        min: 0,
        max: 100
      },
      notifyUsers: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
      }],
      notifyRoles: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Role'
      }]
    }
  },
  metrics: {
    complianceTarget: {
      type: Number,
      default: 95, // Percentage
      min: 0,
      max: 100
    },
    trackResponseTime: {
      type: Boolean,
      default: true
    },
    trackResolutionTime: {
      type: Boolean,
      default: true
    },
    pauseOnStatus: [{
      type: String
    }]
  },
  isActive: {
    type: Boolean,
    default: true
  },
  isDefault: {
    type: Boolean,
    default: false
  },
  applicableTo: {
    categories: [String],
    modules: [String],
    conditions: mongoose.Schema.Types.Mixed
  },
  createdBy: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  }
}, {
  timestamps: true
});

// Indexes
slaSchema.index({ companyId: 1, isActive: 1 });
slaSchema.index({ type: 1 });
slaSchema.index({ isDefault: 1 });

module.exports = mongoose.model('SLA', slaSchema);