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

export interface SeaQALikeAttributes extends SeaQALikeInterface {}
export interface SeaQALikeCreationAttributes extends Optional<SeaQALikeAttributes, 'id'> {}

export class SeaQALike extends Model<SeaQALikeAttributes, SeaQALikeCreationAttributes> implements SeaQALikeAttributes {
    public id!: number;
    public userId!: number;
    public entityId!: number;
    public entityType!: 'question' | 'answer';
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof SeaQALike {
        SeaQALike.init({
            id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
            userId: { type: DataTypes.BIGINT, allowNull: false, comment: "user reference" },
            entityId: { type: DataTypes.BIGINT, allowNull: false, comment: "liked entity id" },
            entityType: { type: DataTypes.STRING, allowNull: false, comment: "question, answer" }
        }, {
            sequelize,
            tableName: 'sea_qa_likes',
            timestamps: true,
            underscored: true,
            indexes: [
                { name: 'sea_qa_like_user_index', fields: ['user_id'] },
                { name: 'sea_qa_like_entity_index', fields: ['entity_id', 'entity_type'] },
                { name: 'sea_qa_like_unique_index', unique: true, fields: ['user_id', 'entity_id', 'entity_type'] }
            ]
        });
        return SeaQALike;
    }

    public static associate(models: any) {
        SeaQALike.belongsTo(models.User, { foreignKey: 'userId', as: 'user' });
    }
}
