import mongoose from 'mongoose';

const payrollSchema = new mongoose.Schema(
  {
    employee: { type: mongoose.Schema.Types.ObjectId, ref: 'Employee', required: true },
    month: { type: Number, required: true, min: 1, max: 12 },
    year: { type: Number, required: true },
    periodLabel: { type: String, required: true },
    basic: { type: Number, default: 0 },
    allowances: { type: Number, default: 0 },
    grossPay: { type: Number, default: 0 },
    deductions: { type: Number, default: 0 },
    netPay: { type: Number, default: 0 },
    status: {
      type: String,
      enum: ['draft', 'processed', 'paid'],
      default: 'draft',
    },
    paidAt: { type: Date },
    notes: { type: String, default: '' },
    breakdown: {
      housing: Number,
      transport: Number,
      medical: Number,
      bonus: Number,
      tax: Number,
      pension: Number,
      insurance: Number,
      loan: Number,
    },
    processedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
  },
  { timestamps: true }
);

payrollSchema.index({ employee: 1, month: 1, year: 1 }, { unique: true });

export default mongoose.model('Payroll', payrollSchema);
