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

module.exports = (sequelize) => {
  const AssetHolding = sequelize.define(
    'AssetHolding',
    {
      id: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },

      userId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        field: 'user_id'
      },

      assetId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        field: 'asset_id'
      },

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

      avgBuyPrice: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
        defaultValue: 0,
        field: 'avg_buy_price'
      },

      totalCost: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
        defaultValue: 0,
        field: 'total_cost'
      },
      dividends: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
        defaultValue: 0,
      },
    },
    {
      tableName: 'asset_holdings',
      timestamps: true,
      underscored: true,
      paranoid: true,
      indexes: [
        { unique: true, fields: ['user_id', 'asset_id'] },
      ],
    }
  );

  return AssetHolding;
};
