module.exports = (sequelize, DataTypes) => {
  const QAMasterComponent = sequelize.define('QAMasterComponent', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    qa_master_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'QAMaster', key: 'id' }
    },
    bom_component_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'BOMComponent', key: 'id' }
    },
    line_no: { type: DataTypes.INTEGER, allowNull: false },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'Material', key: 'id' }
    },
    width: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    width_tol: { type: DataTypes.STRING(50), allowNull: true },
    length: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    length_tol: { type: DataTypes.STRING(50), allowNull: true },
    height: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    height_tol: { type: DataTypes.STRING(50), allowNull: true },
    thickness: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    thickness_tol: { type: DataTypes.STRING(50), allowNull: true },
    tol: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_1: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_2: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_3: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_4: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_5: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_6: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_7: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_8: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_9: { type: DataTypes.STRING(50), allowNull: true },
    pb_dim_10: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_1: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_2: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_3: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_4: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_5: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_6: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_7: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_8: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_9: { type: DataTypes.STRING(50), allowNull: true },
    pb_tol_10: { type: DataTypes.STRING(50), allowNull: true }
  }, {
    tableName: 'qa_master_component',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['qa_master_id'] },
      { fields: ['bom_component_id'] },
      { fields: ['material_id'] },
      { fields: ['line_no'] }
    ]
  });

  QAMasterComponent.associate = function(models) {
    QAMasterComponent.belongsTo(models.QAMaster, { foreignKey: 'qa_master_id', as: 'qaMaster' });
    QAMasterComponent.belongsTo(models.BOMComponent, { foreignKey: 'bom_component_id', as: 'bomComponent' });
    QAMasterComponent.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
  };

  return QAMasterComponent;
};

