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

export interface UserReferralAttributes extends Optional<UserReferralInterface, 'id'> { }

export class UserReferral extends Model<UserReferralInterface, UserReferralAttributes> implements UserReferralInterface {
    public id!: number;
    public userId!: number;
    public referrerId!: number;
    public points!: number;
    public status!: number;

    static initModel(sequelize: Sequelize): typeof UserReferral {
        UserReferral.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                userId: { type: DataTypes.BIGINT, allowNull: false, comment: "ref to User" },
                referrerId: { type: DataTypes.BIGINT, allowNull: false, comment: "user referral" },
                points: { type: DataTypes.INTEGER, allowNull: false, comment: "user referral" },
                status: { type: DataTypes.INTEGER, allowNull: true, defaultValue: 0, comment: "user referral" },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'user_referral',
                timestamps: true,
                indexes: [
                    { name: 'user-referrer', fields: ['user_id'] },
                    { name: 'user-referrer-index', fields: ['referrer_id'] }
                ]
            }
        );
        return UserReferral;
    }

    public static associate(models: any) {
        UserReferral.belongsTo(models.User, { foreignKey: 'userId', as: 'referredUser' });
        UserReferral.belongsTo(models.User, { foreignKey: 'referrerId', as: 'referrerUser' });
    }
}
