const mongoose = require("mongoose");

const payrollSchema = new mongoose.Schema(
  {
    employee: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Employee",
      required: [true, "Employee is required"]
    },
    month: {
      type: Number,
      required: [true, "Month is required"],
      min: 1,
      max: 12
    },
    year: {
      type: Number,
      required: [true, "Year is required"]
    },
    basicPay: {
      type: Number,
      required: [true, "Basic pay is required"],
      min: 0
    },
    allowances: {
      type: Number,
      default: 0,
      min: 0
    },
    deductions: {
      type: Number,
      default: 0,
      min: 0
    },
    netPay: {
      type: Number,
      required: [true, "Net pay is required"],
      min: 0
    },
    paymentDate: {
      type: Date,
      default: Date.now
    }
  },
  { timestamps: true }
);

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

module.exports = mongoose.model("Payroll", payrollSchema);
