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

export interface PostCategoryAttributes extends Optional<PostCategoryInterface, 'id'> { }

export class PostCategory extends Model<PostCategoryInterface, PostCategoryAttributes> implements PostCategoryInterface {
    public id!: number;
    public categoryId!: number;
    public postId!: number;

    static initModel(sequelize: Sequelize): typeof PostCategory {
        PostCategory.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                categoryId: { type: DataTypes.BIGINT, allowNull: false, comment: "ref to Category" },
                postId: { type: DataTypes.BIGINT, allowNull: false, comment: "ref to Post" }
            },
            {
                paranoid: false,
                underscored: true,
                sequelize,
                tableName: 'post_categories',
                timestamps: true,
            }
        );
        return PostCategory;
    }

    public static associate(models: any) {
        PostCategory.belongsTo(models.Category, { foreignKey: 'categoryId' });
        PostCategory.belongsTo(models.Post, { foreignKey: 'postId' });
    }
}
