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

export interface StageApprovalPolicyAttributes {
    id: string;
    stageCode: string;
    minApprovals: number;
    approvalMode: 'ALL' | 'MIN_N' | 'ROLE_MANDATORY';
    requiredRoles: string[];
    isActive: boolean;
}

export interface StageApprovalPolicyInstance extends Model<StageApprovalPolicyAttributes>, StageApprovalPolicyAttributes { }

export default (sequelize: Sequelize) => {
    const StageApprovalPolicy = sequelize.define<StageApprovalPolicyInstance>('StageApprovalPolicy', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        stageCode: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true
        },
        minApprovals: {
            type: DataTypes.INTEGER,
            allowNull: false,
            defaultValue: 1
        },
        approvalMode: {
            type: DataTypes.ENUM('ALL', 'MIN_N', 'ROLE_MANDATORY'),
            allowNull: false,
            defaultValue: 'MIN_N'
        },
        requiredRoles: {
            type: DataTypes.JSON,
            allowNull: false,
            defaultValue: []
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: true
        }
    }, {
        tableName: 'stage_approval_policies',
        timestamps: true,
        indexes: [
            { fields: ['stageCode'], unique: true },
            { fields: ['isActive'] }
        ]
    });

    return StageApprovalPolicy;
};
