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

export interface NotificationAttributes extends Optional<NotificationInterface, 'id'> { }

export class Notification extends Model<NotificationInterface, NotificationAttributes> implements NotificationInterface {
    public id!: number;
    public code!: string;
    public isRevision!: boolean;
    public revisionId!: number;
    public userId!: number;
    public accountId!: number;
    public lastUpdatedBy!: number;
    public replacements!: string;
    public status!: number;
    public sortOrder!: number;
    public imageId!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof Notification {
        Notification.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                userId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user identifier" },
                accountId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user Account identifier" },
                imageId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user image identifier" },
                lastUpdatedBy: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user identifier" },
                code: { type: DataTypes.STRING, allowNull: false, unique: "unique-notification-template", comment: "notification template code" },
                replacements: { type: DataTypes.STRING, allowNull: false, unique: "unique-notification-template", comment: "notification template replacements" },
                isRevision: { type: DataTypes.BOOLEAN, allowNull: true, defaultValue: false, comment: "is revision?" },
                revisionId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "revision ref identifier" },
                sortOrder: { type: DataTypes.INTEGER, defaultValue: 0, comment: "notification template sort order" },
                status: { type: DataTypes.INTEGER, allowNull: true, defaultValue: 1, comment: "notification template's status" }
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'notifications',
                timestamps: true,
                indexes: [
                    { name: 'notification-revision-index', fields: ['is_revision'] },
                    { name: 'notification-index', fields: ['status'] },
                    { name: 'notification-code-index', fields: ['code'] }
                ]

            }
        );
        return Notification;
    }

    public static associate(models: any) {
        Notification.hasOne(models.NotificationContent, { foreignKey: 'notificationId', as: 'defaultContent' });
        Notification.hasOne(models.NotificationContent, { foreignKey: 'notificationId', as: 'content' });
        Notification.hasOne(models.NotificationContent, { foreignKey: 'notificationId', as: 'notificationContent' });
        Notification.hasMany(models.NotificationContent, { foreignKey: 'notificationId', as: 'notificationContents' });
        Notification.belongsTo(models.User, { foreignKey: 'userId', as: 'author' });
        Notification.belongsTo(models.User, { foreignKey: 'lastUpdatedBy', as: 'updatedBy' });
        Notification.belongsTo(models.Attachment, { foreignKey: "imageId", as: "notificationImage" });
    }
}
