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

export interface UserAccountAttributes extends Optional<UserAccountInterface, 'id'> { }

export class UserAccount extends Model<UserAccountInterface, UserAccountAttributes> implements UserAccountInterface {
    public id!: number;
    public userId!: number;
    public accountId!: number;
    public isDefault!: boolean;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof UserAccount {
        UserAccount.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "Unique identifier" },
                userId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-account", comment: "User identifier" },
                accountId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-account", comment: "Account identifier" },
                isDefault: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, comment: "Is default?" },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'user_accounts',
                timestamps: true,
            }
        );
        return UserAccount;
    }

    public static associate(models: any) {
        UserAccount.belongsTo(models.User, {foreignKey: 'userId',as: 'refUser',onDelete: 'cascade',hooks: true});
        UserAccount.belongsTo(models.Account, {foreignKey: 'accountId',as: 'refAccount',onDelete: 'cascade',hooks: true});
    }
}
