module.exports = (sequelize, DataTypes) => {
  const MaterialValuation = sequelize.define('MaterialValuation', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    valuation_area: {
      type: DataTypes.STRING(10),
      allowNull: true,
      comment: 'Valuation area (usually same as plant)'
    },
    valuation_type: {
      type: DataTypes.ENUM('S', 'V', 'T'),
      allowNull: false,
      defaultValue: 'S',
      comment: 'S=Standard, V=Moving Average, T=Periodic Unit Price'
    },
    price_control: {
      type: DataTypes.ENUM('S', 'V'),
      allowNull: false,
      defaultValue: 'S',
      comment: 'S=Standard Price, V=Moving Average Price'
    },
    standard_price: {
      type: DataTypes.DECIMAL(15, 4),
      allowNull: true,
      defaultValue: 0.0000
    },
    moving_average_price: {
      type: DataTypes.DECIMAL(15, 4),
      allowNull: true,
      defaultValue: 0.0000
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    valuation_class: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    costing_lot_size: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      defaultValue: 1.000
    },
    original_price: {
      type: DataTypes.DECIMAL(15, 4),
      allowNull: true,
      defaultValue: 0.0000
    },
    last_price_change_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    effective_from: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    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: 'material_valuation',
    indexes: [
      {
        unique: true,
        fields: ['material_id', 'plant_id', 'valuation_area']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  MaterialValuation.associate = function(models) {
    MaterialValuation.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    MaterialValuation.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    MaterialValuation.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
  };

  return MaterialValuation;
};

