module.exports = (sequelize, DataTypes) => {
  const MeasuringInstrument = sequelize.define('MeasuringInstrument', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      },
      comment: 'Company this instrument belongs to'
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      },
      comment: 'Plant where instrument is located'
    },
    description: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: 'Instrument description (e.g., VERNIER CALIPER, MICROMETER)'
    },
    range: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Measurement range (e.g., 0-300, 0-25)'
    },
    least_count: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Least count / precision (e.g., 0.02, 0.01)'
    },
    make: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Manufacturer name (e.g., MITUTOYO, INSIZE)'
    },
    instrument_id: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'Unique instrument ID (e.g., RI-VC-01, RI-MM-01)'
    },
    serial_no: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Serial number from manufacturer'
    },
    calibrated_on: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Last calibration date'
    },
    due_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Next calibration due date'
    },
    frequency: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Calibration frequency (e.g., 12 Month, 6 Month)'
    },
    location: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Physical location (e.g., BENDING, QUALITY, PowederCoating)'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Additional remarks or notes'
    },
    status: {
      type: DataTypes.ENUM('GOOD', 'CALIBRATION_DUE', 'UNDER_CALIBRATION', 'OUT_OF_SERVICE', 'DAMAGED'),
      allowNull: false,
      defaultValue: 'GOOD',
      comment: 'Current status of the instrument'
    },
    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: 'measuring_instruments',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      {
        fields: ['company_id']
      },
      {
        fields: ['plant_id']
      },
      {
        unique: true,
        fields: ['instrument_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['due_date']
      }
    ]
  });

  MeasuringInstrument.associate = function(models) {
    MeasuringInstrument.belongsTo(models.Company, {
      foreignKey: 'company_id',
      as: 'company'
    });

    MeasuringInstrument.belongsTo(models.Plant, {
      foreignKey: 'plant_id',
      as: 'plant'
    });

    MeasuringInstrument.belongsTo(models.User, {
      foreignKey: 'created_by',
      as: 'creator'
    });

    MeasuringInstrument.belongsTo(models.User, {
      foreignKey: 'updated_by',
      as: 'updater'
    });
  };

  return MeasuringInstrument;
};
