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

export type SlaNotificationDispatchType =
    | 'pre_breach_reminder'
    | 'breach'
    | 'escalation'
    | 'repeat_overdue';

export interface SLANotificationDispatchAttributes {
    id: string;
    trackingId: string;
    /** Unique per tracking row — e.g. reminder:uuid, breach, escalation:1 */
    thresholdKey: string;
    dispatchType: SlaNotificationDispatchType;
    templateCode: string | null;
    stageName: string | null;
    reminderId: string | null;
    escalationLevel: number | null;
    recipientCount: number;
    sentAt: Date;
    status: string;
}

export interface SLANotificationDispatchInstance
    extends Model<SLANotificationDispatchAttributes>,
        SLANotificationDispatchAttributes {}

export default (sequelize: Sequelize) => {
    const SLANotificationDispatch = sequelize.define<SLANotificationDispatchInstance>(
        'SLANotificationDispatch',
        {
            id: {
                type: DataTypes.UUID,
                defaultValue: DataTypes.UUIDV4,
                primaryKey: true
            },
            trackingId: {
                type: DataTypes.UUID,
                allowNull: false,
                references: {
                    model: 'sla_tracking',
                    key: 'id'
                }
            },
            thresholdKey: {
                type: DataTypes.STRING(128),
                allowNull: false
            },
            dispatchType: {
                type: DataTypes.STRING(32),
                allowNull: false
            },
            templateCode: {
                type: DataTypes.STRING(64),
                allowNull: true
            },
            stageName: {
                type: DataTypes.STRING(255),
                allowNull: true
            },
            reminderId: {
                type: DataTypes.UUID,
                allowNull: true
            },
            escalationLevel: {
                type: DataTypes.INTEGER,
                allowNull: true
            },
            recipientCount: {
                type: DataTypes.INTEGER,
                allowNull: false,
                defaultValue: 0
            },
            sentAt: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW
            },
            status: {
                type: DataTypes.STRING(24),
                allowNull: false,
                defaultValue: 'sent'
            }
        },
        {
            tableName: 'sla_notification_dispatches',
            timestamps: true,
            indexes: [
                {
                    unique: true,
                    name: 'sla_notification_dispatches_tracking_threshold_uq',
                    fields: ['trackingId', 'thresholdKey']
                },
                {
                    name: 'sla_notification_dispatches_tracking_sent_idx',
                    fields: ['trackingId', 'sentAt']
                },
                {
                    name: 'sla_notification_dispatches_type_idx',
                    fields: ['dispatchType']
                }
            ]
        }
    );

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

    return SLANotificationDispatch;
};
