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

// const invoiceSchema = new mongoose.Schema(
//   {
//     invoiceNumber: {
//       type: String,
//       trim: true,
//       unique: true,
//       sparse: true,
//     },

//     // 🔗 LINK TO TASK (MOST IMPORTANT)
//     taskId: {
//       type: mongoose.Schema.Types.ObjectId,
//       ref: "Task",
//       required: true,
//       unique: true,
//     },

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

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

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

//     amount: {
//       type: Number,
//       required: true,
//       min: 1,
//     },

//     orgId: {
//       type: mongoose.Schema.Types.ObjectId,
//       ref: "Organization",
//       required: true,
//     },

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

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

//     status: {
//       type: String,
//       enum: ["pending", "in-progress", "paid"],
//       default: "pending",
//     },

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

//     isDeleted: {
//       type: Boolean,
//       default: false,
//     },

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

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


// module.exports = mongoose.model("Invoice", invoiceSchema);
const mongoose = require("mongoose");

const InvoiceSchema = new mongoose.Schema(
  {
    invoiceNumber: { type: String, unique: true },

    // Reference
    taskId: { type: mongoose.Schema.Types.ObjectId, ref: "Task", required: true },
    orgId: { type: mongoose.Schema.Types.ObjectId, ref: "Organization", required: true },

    //  Company snapshot
    company: {
      name: { type: String, required: true },
      address: { type: String, required: true },
      gstin: { type: String, default: null },
      // state: { type: String },
    },

    //  Customer snapshot
    customer: {
      name: { type: String, required: true },
      phone: String,
      email: String,
      // address: String,
      // state: String,
    },

    //  Invoice items
    items: [
      {
        description: { type: String, required: true },
        quantity: { type: Number, default: 1 },
        rate: { type: Number, required: true },
        amount: { type: Number, required: true },
        tax: {
          name: String, // GST 18%
          cgstPercent: Number,
          sgstPercent: Number,
          igstPercent: Number,
          cgstAmount: Number,
          sgstAmount: Number,
          igstAmount: Number,
          totalTax: Number,
        },

        /* ----- DISCOUNT SNAPSHOT ----- */
        discount: {
          name: String,
          type: { type: String, enum: ["PERCENTAGE", "FLAT"] },
          value: Number,
          amount: Number,
        },
        total: { type: Number, required: true }, // final item total


      },
    ],

    //  Totals
    subTotal: { type: Number, required: true },
    // tax: {
    //   percentage: Number,
    //   amount: Number,
    // },

    totalDiscount: { type: Number, default: 0 },
    totalTax: { type: Number, default: 0 },
    grandTotal: { type: Number, required: true },

    //  Payment
    payment: {
      paidAmount: { type: Number, default: 0 },
      method: { type: String, enum: ["cash", "upi", "card", "bank"] },
      transactionId: String,
      paidAt: Date,
    },

    // status: {
    //   type: String,
    //   enum: ["pending", "paid", "cancelled"],
    //   default: "pending",
    // },
    reminder: {
      lastSentAt: Date,
      nextReminderAt: Date,
      reminderCount: { type: Number, default: 0 }
    },
    status: {
      type: String,
      enum: ["DRAFT", "ISSUED", "PAID", "CANCELLED"],
      default: "DRAFT",
    },
    generatedDate: {
      type: Date,
      required: true,
    },

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


    terms: String,
    notes: String,

    isDeleted: { type: Boolean, default: false },

    createdBy: mongoose.Schema.Types.ObjectId,
    updatedBy: mongoose.Schema.Types.ObjectId,
  },
  { timestamps: true }
);

module.exports = mongoose.model("Invoice", InvoiceSchema);
