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

export interface CategoryContentAttributes extends Optional<CategoryContentInterface, 'id'> { }

export class CategoryContent extends Model<CategoryContentInterface, CategoryContentAttributes> implements CategoryContentInterface {
    public id!: number;
    public categoryId!: number;
    public languageId!: number;
    public name!: string;
    public description!: Text;
    public descriptionText!: Text;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

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

    public static associate(models: any) {
        CategoryContent.belongsTo(models.Language, { foreignKey: 'languageId', as: 'language' });
        CategoryContent.belongsTo(models.Category, { foreignKey: 'categoryId', as: 'category' });
    }
}
