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

export interface SLABreachAttributes {
    id: string;
    trackingId: string;
    breachedAt: Date;
    notifiedTo: string | null; // Email or User ID
    status: string; // Open, Acknowledged, Resolved
    actionTaken: string | null;
}

export interface SLABreachInstance extends Model<SLABreachAttributes>, SLABreachAttributes { }

export default (sequelize: Sequelize) => {
    const SLABreach = sequelize.define<SLABreachInstance>('SLABreach', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        trackingId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'sla_tracking',
                key: 'id'
            }
        },
        breachedAt: {
            type: DataTypes.DATE,
            defaultValue: DataTypes.NOW
        },
        notifiedTo: {
            type: DataTypes.STRING,
            allowNull: true
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'Open'
        },
        actionTaken: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        tableName: 'sla_breaches',
        timestamps: true,
        updatedAt: false
    });

    (SLABreach as any).associate = (models: any) => {
        SLABreach.belongsTo(models.SLATracking, { foreignKey: 'trackingId', as: 'slaTracking' });
    };

    return SLABreach;
};
