
export default (sequelize, DataTypes) => {
     return sequelize.define('LeadManagement', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },  
        leadId: {
            type: DataTypes.STRING(10),
            unique: true,
            allowNull: false,
            field: "lead_id",
        },  
        name: {
            type: DataTypes.STRING(100),
            allowNull: false,
            validate: {
                notEmpty: { msg: "Name is required" },
                noDigits(value) {
                    if (/\d/.test(value)) {
                        throw new Error("Name must not contain numbers");
                    }
                }
            },
        },  
        email: {
            type: DataTypes.STRING(150),
            allowNull: false,
            validate: {
                isEmail: { msg: "Please provide a valid email address" },
                notEmpty: { msg: "Email is required" }
            },
        },
        phone: {
            type: DataTypes.STRING(20),
            allowNull: false,
            validate: {
                notEmpty: { msg: "Phone number is required" },
                isValidPhone(value) {
                    if (!/^\+91\s\d{5}\s\d{5}$/.test(value)) {
                        throw new Error("Phone must be in format: +91 XXXXX XXXXX");
                    }
                }
            },
        },
        status: {
            type: DataTypes.ENUM("New", "Contacted", "Qualified", "Disqualified"),
            allowNull: false,
            defaultValue: "New",
            validate: {
                isIn: {
                    args: [["New", "Contacted", "Qualified", "Disqualified"]],
                    msg: "Status must be one of: New, Contacted, Qualified, Disqualified"
                }
            },
        },
        date: {
            type: DataTypes.DATEONLY,
            allowNull: false,
            defaultValue: DataTypes.NOW
        },
        source: {
            type: DataTypes.STRING(100),
            allowNull: false,
            validate: {
                notEmpty: { msg: "Source is required" }
            },
        },
        text_area: {
            type: DataTypes.TEXT,
            allowNull: true
        }
        
    }, {
        tableName: "lead_management",
        underscored: true,
        timestamps: true
    });
};

