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

const paymentSchema = new mongoose.Schema(
    {

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


        paymentNumber: {
            type: String,
            required: true,
            unique: true,
            index: true, // PAY-1001
        },

        /* ================= CUSTOMER ================= */
        // customer: {
        //   type: Object,
        //   required: true,
        //   /*
        //     {
        //       name,
        //       email,
        //       phone
        //     }
        //   */
        // },
        /* ================= CUSTOMER SNAPSHOT ================= */
        customer: {
            name: { type: String, required: true },
            phone: String,
            email: String,
        },

        invoiceId: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Invoice",
            default: null,
            index: true,
        },

        invoiceNumber: {
            type: String, // snapshot
            index: true,
        },

        amount: {
            type: Number,
            required: true,
            min: 0,
        },

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

        paymentDate: {
            type: Date,
            required: true,
        },

        mode: {
            type: String,
            enum: ["CASH", "UPI", "NEFT", "CARD","BANK"],
            required: true,
        },

        reference: {
            type: String, // UTR / Txn ID / Cheque No
            trim: true,
        },

        notes: {
            type: String,
            trim: true,
        },

        status: {
            type: String,
            enum: ["RECEIVED"],
            default: "RECEIVED",
            index: true,
        },

        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("Payment", paymentSchema);
