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

export interface EmailTemplateContentAttributes extends Optional<EmailTemplateContentInterface, 'id'> { }

export class EmailTemplateContent extends Model<EmailTemplateContentInterface, EmailTemplateContentAttributes> implements EmailTemplateContentInterface {
    public id!: number;
    public emailTemplateId!: number;
    public languageId!: number;
    public title!: string;
    public subject!: string;
    public body!: Text;
    public bodyText!: Text;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

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

    public static associate(models: any) {
        EmailTemplateContent.belongsTo(models.EmailTemplate, { foreignKey: 'emailTemplateId', as: 'emailTemplate' });
        EmailTemplateContent.belongsTo(models.Language, { foreignKey: 'languageId', as: 'language' });
    }
}
