module.exports = (sequelize, DataTypes) => {
  const ProductionOrderComponent = sequelize.define('ProductionOrderComponent', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    bom_component_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BOMComponent',
        key: 'id'
      }
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    required_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    issued_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    backflushed_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    issue_type: {
      type: DataTypes.ENUM('MANUAL', 'BACKFLUSH', 'AUTO'),
      allowNull: false,
      defaultValue: 'MANUAL'
    },
    storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    is_substituted: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: 'Whether this component has been substituted'
    },
    original_material_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Material',
        key: 'id'
      },
      comment: 'Original material from BOM (if substituted)'
    },
    substitution_transaction_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'MaterialSubstitutionTransaction',
        key: 'id'
      },
      comment: 'Reference to substitution transaction'
    }
  }, {
    tableName: 'production_order_component',
    indexes: [
      {
        fields: ['production_order_id']
      },
      {
        fields: ['material_id']
      }
    ]
  });

  ProductionOrderComponent.associate = function(models) {
    ProductionOrderComponent.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ProductionOrderComponent.belongsTo(models.BOMComponent, { foreignKey: 'bom_component_id', as: 'bomComponent' });
    ProductionOrderComponent.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    ProductionOrderComponent.belongsTo(models.Material, { foreignKey: 'original_material_id', as: 'originalMaterial' });
    ProductionOrderComponent.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    ProductionOrderComponent.belongsTo(models.MaterialSubstitutionTransaction, { foreignKey: 'substitution_transaction_id', as: 'substitutionTransaction' });
  };

  return ProductionOrderComponent;
};

