module.exports = (sequelize, DataTypes) => {
  const MaterialRequest = sequelize.define('MaterialRequest', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      }
    },
    request_number: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true
    },
    request_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    required_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Header-level required date (optional). Line items may have their own required_date.'
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    request_type: {
      type: DataTypes.ENUM('PRODUCTION_ORDER', 'GENERAL'),
      allowNull: false,
      defaultValue: 'PRODUCTION_ORDER',
      comment: 'PRODUCTION_ORDER = linked to production order, GENERAL = departmental ad-hoc request'
    },
    department_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Department',
        key: 'id'
      },
      comment: 'Department that requested the materials'
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      },
      comment: 'Customer for whom this general material request is raised'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SENT_TO_STORES', 'REVIEWED', 'SENT_TO_PURCHASE', 'PARTIALLY_ISSUED', 'COMPLETED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    sent_to_stores_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    sent_to_stores_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    reviewed_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    reviewed_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    stores_reviewed_issue_at: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Stores refreshed ATP for issue-tab lines'
    },
    stores_reviewed_issue_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'User', key: 'id' }
    },
    stores_reviewed_pr_at: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Stores refreshed ATP for PR-tab lines'
    },
    stores_reviewed_pr_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'User', key: 'id' }
    },
    sent_to_purchase_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    sent_to_purchase_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    purchase_requisition_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'PurchaseRequisition',
        key: 'id'
      }
    },
    remarks: {
      type: DataTypes.TEXT,
      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_request',
    indexes: [
      {
        fields: ['company_id']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['production_order_id']
      },
      {
        fields: ['request_type']
      },
      {
        fields: ['department_id']
      },
      {
        fields: ['customer_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['request_number'],
        unique: true
      }
    ]
  });

  MaterialRequest.associate = function(models) {
    MaterialRequest.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    MaterialRequest.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    MaterialRequest.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    MaterialRequest.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    MaterialRequest.belongsTo(models.Department, { foreignKey: 'department_id', as: 'department' });
    MaterialRequest.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    MaterialRequest.belongsTo(models.PurchaseRequisition, { foreignKey: 'purchase_requisition_id', as: 'purchaseRequisition' });
    MaterialRequest.hasMany(models.MaterialRequestItem, { foreignKey: 'request_id', as: 'items' });
    MaterialRequest.belongsTo(models.User, { foreignKey: 'sent_to_stores_by', as: 'sentToStoresByUser' });
    MaterialRequest.belongsTo(models.User, { foreignKey: 'reviewed_by', as: 'reviewedByUser' });
    MaterialRequest.belongsTo(models.User, { foreignKey: 'stores_reviewed_issue_by', as: 'storesReviewedIssueByUser' });
    MaterialRequest.belongsTo(models.User, { foreignKey: 'stores_reviewed_pr_by', as: 'storesReviewedPrByUser' });
    MaterialRequest.belongsTo(models.User, { foreignKey: 'sent_to_purchase_by', as: 'sentToPurchaseByUser' });
    MaterialRequest.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    MaterialRequest.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    MaterialRequest.hasMany(models.NestingStoresRequest, { foreignKey: 'material_request_id', as: 'nestingStoresRequests' });
  };

  return MaterialRequest;
};

