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

export interface PostContentAttributes extends Optional<PostContentInterface, 'id'> { }

export class PostContent extends Model<PostContentInterface, PostContentAttributes> implements PostContentInterface {
    public id!: number;
    public postId!: number;
    public languageId!: number;
    public title!: string;
    public description!: Text;
    public descriptionText!: Text;
    public embedCode!: Text | null;
    public exerpt!: Text | null;
    public limitedContent!: Text | null;
    public authorName!: string | null;
    public metaTitle!: string | null;
    public metaDescription!: string | null;
    public metaKeywords!: string | null;
    public canonicalUrl!: string | null;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof PostContent {
        PostContent.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                postId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-post-content", comment: "post identifier" },
                languageId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-post-content", comment: "content language identifier" },
                title: { type: DataTypes.STRING, allowNull: false, comment: "post name" },
                description: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "post description html format" },
                descriptionText: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "post description text format" },
                exerpt: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "post description text format" },
                limitedContent: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "post limited content text format" },
                metaTitle: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "seo meta title" },
                metaDescription: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "seo meta description" },
                metaKeywords: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "seo meta keywords" },
                canonicalUrl: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "seo canonical url" },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'post_content',
                timestamps: true,
                indexes: [
                    { name: 'post-search', fields: ['title', 'description_text'], type: 'FULLTEXT' },
                    { name: 'post-language', fields: ['language_id'] }
                ]
            }
        );
        return PostContent;
    }

    public static associate(models: any) {
        PostContent.belongsTo(models.CategoryType, { foreignKey: 'postId', as: 'post' });
        PostContent.belongsTo(models.Language, { foreignKey: 'languageId', as: 'language' });
    }
}
