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

export interface SeaQAQuestionContentAttributes extends Optional<SeaQAQuestionContentInterface, 'id'> { }

export class SeaQAQuestionContent extends Model<SeaQAQuestionContentInterface, SeaQAQuestionContentAttributes> implements SeaQAQuestionContentInterface {
    public id!: number;
    public questionId!: number;
    public languageId!: number;
    public title!: string;
    public description!: string;
    public descriptionText!: string;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof SeaQAQuestionContent {
        SeaQAQuestionContent.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                questionId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-sea-qa-question-content", comment: "question identifier" },
                languageId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-sea-qa-question-content", comment: "content language identifier" },
                title: { type: DataTypes.TEXT, allowNull: false, comment: "question title" },
                description: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "question description html format" },
                descriptionText: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "question description text format" }
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'sea_qa_question_content',
                timestamps: true,
                indexes: [
                    { name: 'sea-qa-question-search', fields: ['title', 'description_text'], type: 'FULLTEXT' },
                    { name: 'sea-qa-question-language', fields: ['language_id'] }
                ]
            }
        );
        return SeaQAQuestionContent;
    }

    public static associate(models: any) {
        SeaQAQuestionContent.belongsTo(models.SeaQAQuestion, { foreignKey: 'questionId', as: 'question' });
        SeaQAQuestionContent.belongsTo(models.Language, { foreignKey: 'languageId', as: 'language' });
    }
}
