    const mongoose = require("mongoose");

    const taxSchema = new mongoose.Schema(
        {
            name: {
                type: String,
                required: [true, "Tax name is required"],
                trim: true,
                maxlength: [50, "Tax name cannot exceed 50 characters"],
                unique: true, // Prevent duplicate GST entries
                index: true,
            },

            type: {
                type: String,
                required: true,
                enum: ["GST", "VAT", "SERVICE_TAX", "CUSTOM"],
                default: "GST",
            },
            rates: {
                cgst: {
                    type: Number,
                    min: [0, "CGST cannot be negative"],
                    max: [100, "CGST cannot exceed 100"],
                    default: 0,
                },
                sgst: {
                    type: Number,
                    min: [0, "SGST cannot be negative"],
                    max: [100, "SGST cannot exceed 100"],
                    default: 0,
                },
                igst: {
                    type: Number,
                    min: [0, "IGST cannot be negative"],
                    max: [100, "IGST cannot exceed 100"],
                    default: 0,
                },
            },

            totalRate: {
                type: Number,
                min: 0,
                max: 100,
            },
            applicableFor: {
                type: String,
                enum: ["INTRA_STATE", "INTER_STATE", "BOTH"],
                default: "BOTH",
            },
            isActive: {
                type: Boolean,
                default: true,
                index: true,
            },

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

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

            createdBy: {
                type: mongoose.Schema.Types.ObjectId,
                ref: "User",
                required: true,
            },
            updatedBy: {
                type: mongoose.Schema.Types.ObjectId,
                ref: "User",
                default: null,
            },
            isDeleted: {
                type: Boolean,
                default: false,
                index: true,
            },
        },
        {
            timestamps: true,
        }
    );
    // Auto-calculate total tax
    taxSchema.pre("save", function (next) {
        const { cgst = 0, sgst = 0, igst = 0 } = this.rates;

        // GST rule: Either CGST+SGST OR IGST
        if (igst > 0 && (cgst > 0 || sgst > 0)) {
            return next(new Error("IGST cannot be combined with CGST/SGST"));
        }

        this.totalRate = igst > 0 ? igst : cgst + sgst;
        next();
    });
    taxSchema.index({ orgId: 1, name: 1 }, { unique: true });

    module.exports = mongoose.model("Tax", taxSchema);
