module.exports = (sequelize, DataTypes) => {
  const BOMHeader = sequelize.define('BOMHeader', {
    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'
      }
    },
    bom_number: {
      type: DataTypes.STRING(40),
      allowNull: false
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    parent_bom_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BOMHeader',
        key: 'id'
      }
    },
    bom_type: {
      type: DataTypes.ENUM('PRODUCTION', 'ENGINEERING', 'SALES', 'PLANT_MAINTENANCE'),
      allowNull: false,
      defaultValue: 'PRODUCTION'
    },
    alternative: {
      type: DataTypes.STRING(2),
      allowNull: true,
      defaultValue: '01'
    },
    base_quantity: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: false,
      defaultValue: 1.000
    },
    base_uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    valid_from: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    valid_to: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    drawing_number: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Drawing number for the assembly (e.g., RCAB00958R4-01-01)'
    },
    revision_number: {
      type: DataTypes.STRING(20),
      allowNull: true,
      comment: 'Revision number for the BOM (e.g., REV 04)'
    },
    width: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Default width from finished material master'
    },
    height: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Default height from finished material master'
    },
    length: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Default length from finished material master'
    },
    thickness: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Default thickness from finished material master'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'LOCKED'),
      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_header',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'bom_number', 'alternative']
      },
      {
        fields: ['material_id']
      },
      {
        fields: ['parent_bom_id']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['valid_from', 'valid_to']
      }
    ]
  });

  BOMHeader.associate = function(models) {
    BOMHeader.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    BOMHeader.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    BOMHeader.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    BOMHeader.belongsTo(models.BOMHeader, { foreignKey: 'parent_bom_id', as: 'parentBOM' });
    BOMHeader.hasMany(models.BOMHeader, { foreignKey: 'parent_bom_id', as: 'childBOMs' });
    BOMHeader.hasMany(models.BOMComponent, { foreignKey: 'bom_id', as: 'components' });
    BOMHeader.belongsTo(models.UOM, { foreignKey: 'base_uom_id', as: 'baseUOM' });
  };

  return BOMHeader;
};

