import { Model, DataTypes, Sequelize } from 'sequelize';

export interface DocumentStageConfigAttributes {
    id: string;
    documentType: string;
    stageCode: string;
    allowedRoles: string[]; // Role codes allowed to view/upload
    isMandatory: boolean;
    module: string; // ONBOARDING, RESIGNATION, RELOCATION, etc.
    description: string | null;
    isActive: boolean;
}

export interface DocumentStageConfigInstance extends Model<DocumentStageConfigAttributes>, DocumentStageConfigAttributes { }

export default (sequelize: Sequelize) => {
    const DocumentStageConfig = sequelize.define<DocumentStageConfigInstance>('DocumentStageConfig', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        documentType: {
            type: DataTypes.STRING,
            allowNull: false
        },
        stageCode: {
            type: DataTypes.STRING,
            allowNull: false,
            defaultValue: 'General'
        },
        allowedRoles: {
            type: DataTypes.JSON,
            allowNull: false,
            defaultValue: []
        },
        isMandatory: {
            type: DataTypes.BOOLEAN,
            defaultValue: false
        },
        description: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            defaultValue: true
        },
        module: {
            type: DataTypes.STRING,
            allowNull: false,
            defaultValue: 'ONBOARDING'
        }
    }, {
        tableName: 'document_stage_configs',
        timestamps: true,
        indexes: [
            { fields: ['stageCode'] },
            { fields: ['documentType'] },
            { fields: ['isActive'] },
            { fields: ['module'] }
        ]
    });

    return DocumentStageConfig;
};
