module.exports = (sequelize, DataTypes) => {
  const IncomingInspectionReportDetail = sequelize.define('IncomingInspectionReportDetail', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    iir_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'IncomingInspectionReport',
        key: 'id'
      }
    },
    line_no: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    parameter: {
      type: DataTypes.STRING(200),
      allowNull: false
    },
    specification: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    instrument: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    instrument_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'MeasuringInstrument',
        key: 'id'
      }
    },
    observation_1: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    observation_2: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    observation_3: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    observation_4: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    observation_5: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    observation_6: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    observation_7: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    observation_8: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    result: {
      type: DataTypes.ENUM('OK', 'NOT_OK', 'N/A'),
      allowNull: true
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'incoming_inspection_report_detail',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['iir_id'] },
      { unique: true, fields: ['iir_id', 'line_no'] }
    ]
  });

  IncomingInspectionReportDetail.associate = function(models) {
    IncomingInspectionReportDetail.belongsTo(models.IncomingInspectionReport, {
      foreignKey: 'iir_id',
      as: 'header'
    });

    IncomingInspectionReportDetail.belongsTo(models.MeasuringInstrument, {
      foreignKey: 'instrument_id',
      as: 'measuringInstrument'
    });
  };

  return IncomingInspectionReportDetail;
};
