module.exports = (sequelize, DataTypes) => {
  const MeasuringInstrumentHistoryCardDetail = sequelize.define('MeasuringInstrumentHistoryCardDetail', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    history_card_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'MeasuringInstrumentHistoryCard', key: 'id' },
      onDelete: 'CASCADE'
    },
    line_no: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    calibration_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    calibrated_at: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    reason_for_interval_change: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    report_no_date: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    acceptance_char_1: {
      type: DataTypes.DECIMAL(15, 5),
      allowNull: true
    },
    acceptance_char_2: {
      type: DataTypes.DECIMAL(15, 5),
      allowNull: true
    },
    acceptance_char_3: {
      type: DataTypes.DECIMAL(15, 5),
      allowNull: true
    },
    acceptance_char_4: {
      type: DataTypes.DECIMAL(15, 5),
      allowNull: true
    },
    next_calibration_due_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    remarks_signature: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'measuring_instrument_history_card_detail',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['history_card_id'] },
      { fields: ['line_no'] }
    ]
  });

  MeasuringInstrumentHistoryCardDetail.associate = function(models) {
    MeasuringInstrumentHistoryCardDetail.belongsTo(models.MeasuringInstrumentHistoryCard, { foreignKey: 'history_card_id', as: 'historyCard' });
  };

  return MeasuringInstrumentHistoryCardDetail;
};
