// models/accounts/expenseModel.js
const mongoose = require("mongoose");

const expenseSchema = new mongoose.Schema(
  {
    /* ================= MULTI TENANCY ================= */
    orgId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Organization",
      required: true,
      index: true,
    },

    /* ================= IDENTIFIERS ================= */
    expenseNumber: {
      type: String,
      required: true,
      unique: true,
      index: true, // EXP-1001
    },

    /* ================= EMPLOYEE ================= */
    // employee: {
    //   type: mongoose.Schema.Types.ObjectId,
    //   ref: "User",
    //   required: true,
    // },

    /* ================= CLASSIFICATION ================= */
    category: {
      type: String,
      required: true, // Office / Travel / Tech
      trim: true,
    },

    expenseName: {
      type: String,
      // required: true,
      trim: true,
      // minlength: 3,
      // maxlength: 100,
    },


    /* ================= FINANCIAL ================= */
    amount: {
      type: Number,
      required: true,
      min: 0,
    },

    currency: {
      type: String,
      default: "INR",
    },

    /* ================= DATES ================= */
    expenseDate: {
      type: Date, // Date incurred
      required: true,
    },

    requestedDate: {
      type: Date,
      default: Date.now, // auto set when created
    },

    /* ================= STATUS ================= */
    status: {
      type: String,
      enum: ["PENDING", "PAID", "REJECTED","APPROVED"],
      default: "PAID",
      index: true,
    },

    /* ================= OPTIONAL INFO ================= */
    notes: {
      type: String,
      trim: true,
    },

    attachments: [
      {
        url: String,
        fileType: String,
      },
    ],

    /* ================= AUDIT ================= */
    createdBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },

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

    isDeleted: {
      type: Boolean,
      default: false,
      index: true,
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model("Expense", expenseSchema);


// const mongoose = require("mongoose");

// const expenseSchema = new mongoose.Schema(
//   {
//     /* ================= MULTI TENANCY ================= */
//     orgId: {
//       type: mongoose.Schema.Types.ObjectId,
//       ref: "Organization",
//       required: true,
//       index: true,
//     },

//     /* ================= BASIC INFO ================= */
//     expenseNumber: {
//       type: String,
//       unique: true,
//       index: true,
//     },

//     title: {
//       type: String,
//       required: true,
//       trim: true,
//     },

//     description: {
//       type: String,
//       trim: true,
//     },

//     /* ================= EMPLOYEE / VENDOR ================= */
//     employee: {
//       type: mongoose.Schema.Types.ObjectId,
//       ref: "User",
//     },

//     vendorName: {
//       type: String,
//       trim: true,
//     },

//     /* ================= CATEGORY ================= */
//     // category: {
//     //   type: mongoose.Schema.Types.ObjectId,
//     //   ref: "ExpenseCategory",
//     //   required: true,
//     // },
//     category: {
//       type: String, // free text or linked to a category master
//       trim: true,
//     },

//     subCategory: {
//       type: String,
//       trim: true,
//     },

//     /* ================= FINANCIAL ================= */
//     amount: {
//       type: Number,
//       required: true,
//       min: 0,
//     },

//     currency: {
//       type: String,
//       default: "INR",
//     },

//     expenseDate: {
//       type: Date,
//       required: true,
//     },

//     /* ================= REIMBURSEMENT ================= */
//     reimbursementType: {
//       type: String,
//       enum: ["REIMBURSABLE", "NON_REIMBURSABLE"],
//       required: true,
//     },

//     /* ================= PAYMENT ================= */
//     paymentMethod: {
//       type: String,
//       enum: ["cash", "bank", "upi", "credit_card", "debit_card", "cheque"],
//     },

//     paymentReference: {
//       type: String,
//       trim: true,
//     },

//     paidAt: Date,

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

//     /* ================= TAX ================= */
//     taxAmount: {
//       type: Number,
//       default: 0,
//     },

//     gstType: {
//       type: String,
//       enum: ["CGST_SGST", "IGST", "NONE"],
//       default: "NONE",
//     },

//     gstPercentage: Number,

//     vendorGSTIN: String,

//     /* ================= ATTACHMENTS ================= */
//     attachments: [
//       {
//         url: String,
//         fileType: String,
//       },
//     ],

//     /* ================= WORKFLOW ================= */
//     status: {
//       type: String,
//       enum: [
//         "DRAFT",
//         "SUBMITTED",
//         "PENDING_APPROVAL",
//         "APPROVED",
//         "REJECTED",
//         "PAID",
//         "CANCELLED",
//       ],
//       default: "DRAFT",
//       index: true,
//     },

//     approvedBy: {
//       type: mongoose.Schema.Types.ObjectId,
//       ref: "User",
//     },

//     approvedAt: Date,

//     rejectionReason: String,

//     /* ================= AUDIT ================= */
//     createdBy: {
//       type: mongoose.Schema.Types.ObjectId,
//       ref: "User",
//       required: true,
//     },

//     updatedBy: {
//       type: mongoose.Schema.Types.ObjectId,
//       ref: "User",
//       required: true,
//     },

//     isDeleted: {
//       type: Boolean,
//       default: false,
//     },
//   },
//   { timestamps: true }
// );

// module.exports = mongoose.model("Expense", expenseSchema);


