module.exports = (sequelize, DataTypes) => {
  const ProductionOrder = sequelize.define('ProductionOrder', {
    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'
      }
    },
    customer_po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CustomerPO',
        key: 'id'
      }
    },
    customer_po_item_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CustomerPOItem',
        key: 'id'
      }
    },
    production_order_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    production_order_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    finished_material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    bom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BOMHeader',
        key: 'id'
      }
    },
    routing_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Routing',
        key: 'id'
      }
    },
    order_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    standard_batch_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true
    },
    batch_planning_enabled: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    start_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    finish_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    confirmed_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    scrapped_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'RELEASED', 'IN_PROGRESS', 'PARTIALLY_CONFIRMED', 'CONFIRMED', 'CLOSED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    released_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    released_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    closed_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    closed_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    is_sent_to_nesting: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    sent_to_nesting_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    sent_to_nesting_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    nesting_number: {
      type: DataTypes.STRING(30),
      allowNull: true
    }
  }, {
    tableName: 'production_order',
    indexes: [
      {
        fields: ['company_id', 'plant_id', 'fiscal_year_id']
      },
      {
        fields: ['production_order_number']
      },
      {
        fields: ['production_order_date']
      },
      {
        fields: ['finished_material_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['customer_po_id']
      },
      {
        fields: ['customer_po_item_id']
      },
      {
        fields: ['nesting_number']
      }
    ]
  });

  ProductionOrder.associate = function(models) {
    ProductionOrder.hasMany(models.ProductionOrderRoutingExecution, {
      foreignKey: 'production_order_id',
      as: 'routingExecutions'
    });
    ProductionOrder.hasMany(models.ProductionNesting, {
      foreignKey: 'production_order_id',
      as: 'nestingEntries'
    });
    ProductionOrder.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionOrder.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ProductionOrder.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    ProductionOrder.belongsTo(models.CustomerPO, { foreignKey: 'customer_po_id', as: 'customerPO' });
    ProductionOrder.belongsTo(models.CustomerPOItem, { foreignKey: 'customer_po_item_id', as: 'customerPOItem' });
    ProductionOrder.belongsTo(models.Material, { foreignKey: 'finished_material_id', as: 'finishedMaterial' });
    ProductionOrder.belongsTo(models.BOMHeader, { foreignKey: 'bom_id', as: 'bom' });
    ProductionOrder.belongsTo(models.Routing, { foreignKey: 'routing_id', as: 'routing' });
    ProductionOrder.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    ProductionOrder.hasMany(models.ProductionOrderComponent, { foreignKey: 'production_order_id', as: 'components' });
    ProductionOrder.hasMany(models.ProductionOrderBatch, { foreignKey: 'production_order_id', as: 'batches' });
    ProductionOrder.hasMany(models.NestingStoresRequest, { foreignKey: 'production_order_id', as: 'nestingStoresRequests' });
    ProductionOrder.hasMany(models.ProductionNestingConfirmation, {
      foreignKey: 'production_order_id',
      as: 'nestingConfirmations'
    });
    ProductionOrder.hasMany(models.MaterialIssue, { foreignKey: 'production_order_id', as: 'materialIssues' });
    ProductionOrder.hasMany(models.ProductionOrderMaterialReceiptLine, {
      foreignKey: 'production_order_id',
      as: 'materialReceiptLines',
    });
    ProductionOrder.hasMany(models.ProductionWipBalance, { foreignKey: 'production_order_id', as: 'wipBalances' });
    ProductionOrder.hasMany(models.ProductionConfirmation, { foreignKey: 'production_order_id', as: 'confirmations' });
    ProductionOrder.hasMany(models.ProductionCost, { foreignKey: 'production_order_id', as: 'costs' });
    ProductionOrder.hasOne(models.ProductionCostVariance, { foreignKey: 'production_order_id', as: 'costVariance' });
    ProductionOrder.belongsTo(models.User, { foreignKey: 'released_by', as: 'releasedByUser' });
    ProductionOrder.belongsTo(models.User, { foreignKey: 'closed_by', as: 'closedByUser' });
    ProductionOrder.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    ProductionOrder.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    ProductionOrder.belongsTo(models.User, { foreignKey: 'sent_to_nesting_by', as: 'sentToNestingByUser' });
  };

  return ProductionOrder;
};

