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

export interface NotificationContentAttributes extends Optional<NotificationContentInterface, 'id'> { }

export class NotificationContent extends Model<NotificationContentInterface, NotificationContentAttributes> implements NotificationContentInterface {
    public id!: number;
    public notificationId!: number;
    public languageId!: number;
    public title!: string;
    public body!: Text;
    public bodyText!: Text;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof NotificationContent {
        NotificationContent.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                notificationId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-notification-template-content", comment: "notification identifier" },
                languageId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-notification-template-content", comment: "content language identifier" },
                title: { type: DataTypes.STRING, allowNull: false, comment: "notification title" },
                body: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "notification body html format" },
                bodyText: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "notification body text format" }
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'notification_content',
                timestamps: true,
                indexes: [
                    { name: 'notification-search', fields: ['title', 'body_text'], type: 'FULLTEXT' },
                    { name: 'notification-language', fields: ['language_id'] }
                ]
            }
        );
        return NotificationContent;
    }

    public static associate(models: any) {
        NotificationContent.belongsTo(models.Notification, { foreignKey: 'notificationId', as: 'notification' });
        NotificationContent.belongsTo(models.Language, { foreignKey: 'languageId', as: 'language' });
    }
}
