module.exports = (sequelize, DataTypes) => {
  const BOMComponent = sequelize.define('BOMComponent', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    bom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BOMHeader',
        key: 'id'
      }
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    item_number: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    component_quantity: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: false,
      defaultValue: 0.000
    },
    component_uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    rate: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      defaultValue: 0.000,
      comment: 'Unit rate auto-fetched from material master'
    },
    component_weight: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Defaulted from material master and editable in BOM'
    },
    component_height: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Defaulted from material master and editable in BOM'
    },
    component_length: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Defaulted from material master and editable in BOM'
    },
    component_thickness: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Defaulted from material master and editable in BOM'
    },
    component_material_grade: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Defaulted from material master and editable in BOM'
    },
    scrap_percentage: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      defaultValue: 0.00
    },
    item_category: {
      type: DataTypes.ENUM('L', 'N', 'R', 'B'),
      allowNull: true,
      comment: 'L=Stock Item, N=Non-stock Item, R=Variable-size Item, B=Text Item'
    },
    operation_number: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Reference to routing operation'
    },
    fixed_quantity: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: 'If true, quantity is fixed regardless of base quantity'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    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: 'bom_component',
    indexes: [
      {
        unique: true,
        fields: ['bom_id', 'item_number']
      },
      {
        fields: ['material_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  BOMComponent.associate = function(models) {
    BOMComponent.belongsTo(models.BOMHeader, { foreignKey: 'bom_id', as: 'bom' });
    BOMComponent.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    BOMComponent.belongsTo(models.UOM, { foreignKey: 'component_uom_id', as: 'componentUOM' });
  };

  return BOMComponent;
};

