const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
  const UserBalance = sequelize.define(
    'UserBalance',
    {
      id: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      reference: {
        type: DataTypes.STRING(16),
        allowNull: false
      },
      userId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id',
        },
        field: "user_id"
      },

      balance: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
        defaultValue: 0,
      },
      demoBalance: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
        defaultValue: 0,
      },

      profit: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
        defaultValue: 0,
      },


      bonus: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
        defaultValue: 0,
      },
    },
    {
      tableName: 'user_balance',
      underscored: true,
      timestamps: true,
      indexes: [
        { fields: ['user_id'], unique: true }, // one balance row per user
      ],
    }
  );

  return UserBalance;
};
