module.exports = (sequelize, DataTypes) => {
  const Currency = sequelize.define('Currency', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    currency_code: {
      type: DataTypes.STRING(3),
      allowNull: false,
      unique: true
    },
    currency_name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    currency_symbol: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    decimal_places: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 2
    },
    is_base_currency: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'currency',
    indexes: [
      {
        unique: true,
        fields: ['currency_code']
      },
      {
        fields: ['status']
      }
    ]
  });

  Currency.associate = function(models) {
    Currency.hasMany(models.CurrencyRate, { foreignKey: 'currency_id', as: 'currencyRates' });
  };

  return Currency;
};

