module.exports = (sequelize, DataTypes) => {
  const ScrapReasonMaster = sequelize.define('ScrapReasonMaster', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    reason_code: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    reason_name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      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: 'scrap_reason_master',
    indexes: [
      { fields: ['company_id', 'reason_code'], unique: true },
      { fields: ['is_active'] },
      { fields: ['status'] }
    ]
  });

  ScrapReasonMaster.associate = function(models) {
    ScrapReasonMaster.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ScrapReasonMaster.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    ScrapReasonMaster.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    ScrapReasonMaster.hasMany(models.ScrapReporting, { foreignKey: 'scrap_reason_id', as: 'scrapReports' });
  };

  return ScrapReasonMaster;
};

