const mongoose = require("mongoose");

const customerSchema = new mongoose.Schema(
    {
        // customerNumber: {
        //     type: String,
        //     unique: true,
        //     index: true,
        //     default: () => `CUST-${Date.now()}`,
        // },

        name: {
            type: String,
            required: true,
            trim: true,
            minlength: 2,
            maxlength: 100,
        },

        email: {
            type: String,
            trim: true,
            lowercase: true,
            match: [/^\S+@\S+\.\S+$/, "Invalid email"],
        },

        phone: {
            type: String,
            required : true,
            trim: true,
            match: [/^[0-9]{10}$/, "Phone must be 10 digits"],
        },

        address: {
            type: String,
            trim: true,
            maxlength: 300,
        },

        // location: {
        //     type: String,
        //     trim: true,
        // },
        state: {
            type: String,
            // required: null,
            trim: true,
            default : null,
        },
        city: {
            type: String,
            // required: ,
            trim: true,
            default : null,
        },

        // For field executives
        primaryContactPerson: {
            type: String,
            trim: true,
        },

        status: {
            type: String,
            enum: ["Active", "Inactive"],
            default: "Active",
        },

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

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

        createdBy: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User",
        },
        updatedBy: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User",
        },
        isActive: {
            type: Boolean,
            default: true,
        },

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

module.exports = mongoose.model("Customer", customerSchema);
