module.exports = (sequelize, DataTypes) => {
  const InProgressMaterialRequest = sequelize.define('InProgressMaterialRequest', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    company_id: { type: DataTypes.INTEGER, allowNull: false },
    plant_id: { type: DataTypes.INTEGER, allowNull: false },
    fiscal_year_id: { type: DataTypes.INTEGER, allowNull: false },
    production_order_id: { type: DataTypes.INTEGER, allowNull: false },
    customer_po_id: { type: DataTypes.INTEGER, allowNull: true },
    ipmr_number: { type: DataTypes.STRING(50), allowNull: false, unique: true },
    reference_production_no: { type: DataTypes.STRING(80), allowNull: true },
    item_code: { type: DataTypes.STRING(100), allowNull: true },
    item_name: { type: DataTypes.STRING(255), allowNull: true },
    customer_name: { type: DataTypes.STRING(255), allowNull: true },
    order_qty: { type: DataTypes.DECIMAL(18, 3), allowNull: true, defaultValue: 0 },
    required_date: { type: DataTypes.DATEONLY, allowNull: true },
    material_request_id: { type: DataTypes.INTEGER, allowNull: true },
    material_request_number: { type: DataTypes.STRING(50), allowNull: true },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'SENT_TO_MR', 'PARTIAL', 'COMPLETED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: { type: DataTypes.TEXT, allowNull: true },
    created_by: { type: DataTypes.INTEGER, allowNull: true },
    updated_by: { type: DataTypes.INTEGER, allowNull: true },
  }, {
    tableName: 'in_progress_material_request',
  });

  InProgressMaterialRequest.associate = function(models) {
    InProgressMaterialRequest.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    InProgressMaterialRequest.belongsTo(models.CustomerPO, { foreignKey: 'customer_po_id', as: 'customerPO' });
    InProgressMaterialRequest.belongsTo(models.MaterialRequest, { foreignKey: 'material_request_id', as: 'materialRequest' });
    InProgressMaterialRequest.hasMany(models.InProgressMaterialRequestItem, { foreignKey: 'in_progress_request_id', as: 'items' });
  };

  return InProgressMaterialRequest;
};

