module.exports = (sequelize, DataTypes) => {
  const WorkOrder = sequelize.define(
    'WorkOrder',
    {
      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' }
      },
      work_order_number: {
        type: DataTypes.STRING(50),
        allowNull: false
      },
      production_order_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'ProductionOrder', key: 'id' }
      },
      routing_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'Routing', key: 'id' }
      },
      customer_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'BusinessPartner', key: 'id' }
      },
      finished_material_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'Material', key: 'id' }
      },
      order_quantity: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: true
      },
      batch_quantity: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: true
      },
      work_order_quantity: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      planned_start_date: {
        type: DataTypes.DATEONLY,
        allowNull: true
      },
      planned_end_date: {
        type: DataTypes.DATEONLY,
        allowNull: true
      },
      priority: {
        type: DataTypes.ENUM('LOW', 'MEDIUM', 'HIGH'),
        allowNull: false,
        defaultValue: 'MEDIUM'
      },
      status: {
        type: DataTypes.ENUM('PLANNED', 'RELEASED', 'IN_PROGRESS', 'COMPLETED', 'CLOSED'),
        allowNull: false,
        defaultValue: 'PLANNED'
      },
      created_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      },
      approved_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      },
      remarks: {
        type: DataTypes.TEXT,
        allowNull: true
      }
    },
    {
      tableName: 'work_order',
      indexes: [
        { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
        { fields: ['work_order_number'] },
        { fields: ['production_order_id'] },
        { fields: ['status'] }
      ]
    }
  );

  WorkOrder.associate = function (models) {
    WorkOrder.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    WorkOrder.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    WorkOrder.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    WorkOrder.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    WorkOrder.belongsTo(models.Routing, { foreignKey: 'routing_id', as: 'routing' });
    WorkOrder.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    WorkOrder.belongsTo(models.Material, { foreignKey: 'finished_material_id', as: 'finishedMaterial' });
    WorkOrder.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    WorkOrder.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
    WorkOrder.hasOne(models.RouteCard, { foreignKey: 'work_order_id', as: 'routeCard' });
    WorkOrder.hasMany(models.WorkOrderOperationPlan, { foreignKey: 'work_order_id', as: 'operations' });
  };

  return WorkOrder;
};

