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

export interface UserFollowerAttributes extends Optional<UserFollowerInterface, 'id'> { }

export class UserFollower extends Model<UserFollowerInterface, UserFollowerAttributes> implements UserFollowerInterface {
    public id!: number;
    public followerId!: number;
    public followingId!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof UserFollower {
        UserFollower.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                followerId: { type: DataTypes.BIGINT, allowNull: false, comment: "ref to User who follows" },
                followingId: { type: DataTypes.BIGINT, allowNull: false, comment: "ref to User who is followed" }
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'user_followers',
                timestamps: true,
                indexes: [
                    { name: 'unique-follower-following', fields: ['follower_id', 'following_id'], unique: true },
                    { name: 'user-following', fields: ['following_id'] },
                    { name: 'user-follower', fields: ['follower_id'] }
                ]
            }
        );
        return UserFollower;
    }

    public static associate(models: any) {
        UserFollower.belongsTo(models.User, { foreignKey: 'followerId', as: 'follower' });
        UserFollower.belongsTo(models.User, { foreignKey: 'followingId', as: 'following' });
    }
}
