const { mongoose } = require("mongoose");

const payrollSchema = new mongoose.Schema(
  {
    payrollId: {
      type: String,
      unique: true,
      required: true
    },
    payrollName: {
      type: String,
      // required: [true, "Payroll name is required"],
      trim: true,
      minlength: [3, "Payroll name must be at least 3 characters long"],
      maxlength: [150, "Payroll name cannot exceed 150 characters"],
    },

    orgId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Organization",
      required: [true, "Organization ID is required"],
    },

    // Payroll period details
    payPeriodStart: { type: Date, required: false },
    payPeriodEnd: { type: Date, required: false },
    payDate: { type: Date, required: true },
    nextPayDate: { type: Date },
    payrollCycle: {
      type: String,
      enum: ["monthly", "bi-weekly", "weekly", "daily"],
      default: "monthly",
    },

    payrollMonth: {
      type: Number, // 1–12
      // required: true,
    },

    payrollYear: {
      type: Number, // 2025
      // required: true,
    },

    // Employees covered in this payroll
    employees: [
      {
        employeeId: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "User",
          // required: true,
        },
        employeeName: String, // optional (denormalized for reports)
        role: {
          type: String,
          required: true,
        },

        // Earnings (flexible list)
        earnings: [
          {
            name: { type: String, required: true }, // e.g. "Basic Salary", "HRA", "Overtime"
            amount: { type: Number, default: 0 },
          },
        ],

        // Deductions (flexible list)
        deductions: [
          {
            name: { type: String, required: true }, // e.g. "Tax", "Loan", "Insurance"
            amount: { type: Number, default: 0 },
          },
        ],

        // Totals
        grossEarnings: { type: Number, default: 0 },
        totalDeductions: { type: Number, default: 0 },
        netPay: { type: Number, default: 0 },

        // Payment details
        paymentMethod: {
          type: String,
          enum: ["bank_transfer", "cash", "check"],
          default: "bank_transfer",
        },
        bankAccount: String,
        transactionId: String,

        // Payslip info
        payslipNumber: { type: String },
        currency: { type: String, default: "INR" },
        notes: String,
      },
    ],


    // Payroll status
    status: {
      type: String,
      enum: ["draft", "processed", "paid", "pending"],
      default: "draft",
    },
    isDeleted: { type: Boolean, default: false },
    approvedBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
    approvedAt: { type: Date },

    paidAt: Date,
    paidBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
    },

    createdBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
    updatedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
  },
  {
    timestamps: true,
  }
);

// payrollSchema.index(
//   { orgId: 1, payrollMonth: 1, payrollYear: 1 },
//   { unique: true }
// );

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