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

export interface SLAEscalationConfigAttributes {
    id: string;
    slaConfigId: string;
    level: number;
    timeValue: number;
    timeUnit: 'hours' | 'days';
    notifyEmail: string | null;
    notifyRole: string | null;
}

export interface SLAEscalationConfigInstance extends Model<SLAEscalationConfigAttributes>, SLAEscalationConfigAttributes { }

export default (sequelize: Sequelize) => {
    const SLAEscalationConfig = sequelize.define<SLAEscalationConfigInstance>('SLAEscalationConfig', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        slaConfigId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'sla_configurations',
                key: 'id'
            }
        },
        level: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        timeValue: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        timeUnit: {
            type: DataTypes.ENUM('hours', 'days'),
            allowNull: false
        },
        notifyEmail: {
            type: DataTypes.STRING,
            allowNull: true,
            validate: { isEmail: true }
        },
        notifyRole: {
            type: DataTypes.STRING,
            allowNull: true
        }
    }, {
        tableName: 'sla_config_escalations',
        timestamps: true,
        indexes: [
            { fields: ['slaConfigId'] }
        ]
    });

    (SLAEscalationConfig as any).associate = (models: any) => {
        SLAEscalationConfig.belongsTo(models.SLAConfiguration, {
            foreignKey: 'slaConfigId',
            as: 'slaConfig'
        });
    };

    return SLAEscalationConfig;
};
