const mongoose = require("mongoose");

const formConfigSchema = new mongoose.Schema({
    key: {
        type: String,
        required: true,
        unique: true, // e.g. "CREATE_CUSTOMER_FORM"
    },

    title: String, // "Create Customer"

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

    fields: [
        {
            name: { type: String, required: true }, // "name"
            label: { type: String, required: true }, // "Customer Name"
            type: {
                type: String,
                enum: ["text", "number", "email", "textarea"],
                required: true,
            },
            placeholder: String,
            required: { type: Boolean, default: false },
            minLength: Number,
            maxLength: Number,
        },
    ],

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

    isActive: { type: Boolean, default: true },
    createdBy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
    },
    // updatedAt: { type: Date, default: Date.now },
},
    {
        timestamps: true,
    });

module.exports = mongoose.model("FormConfig", formConfigSchema);
