module.exports = (sequelize, DataTypes) => {
  const ScrapReporting = sequelize.define('ScrapReporting', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      }
    },
    scrap_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    scrap_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    scrap_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    scrap_reason: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    scrap_reason_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'ScrapReasonMaster',
        key: 'id'
      }
    },
    batch_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    lot_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    approved_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    approved_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    work_center_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'WorkCenter',
        key: 'id'
      }
    },
    operation_number: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'POSTED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    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_reporting',
    indexes: [
      {
        fields: ['company_id', 'plant_id', 'fiscal_year_id']
      },
      {
        fields: ['scrap_number']
      },
      {
        fields: ['production_order_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  ScrapReporting.associate = function(models) {
    ScrapReporting.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ScrapReporting.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ScrapReporting.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    ScrapReporting.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ScrapReporting.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    ScrapReporting.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    ScrapReporting.belongsTo(models.ScrapReasonMaster, { foreignKey: 'scrap_reason_id', as: 'scrapReason' });
    ScrapReporting.belongsTo(models.WorkCenter, { foreignKey: 'work_center_id', as: 'workCenter' });
    ScrapReporting.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    ScrapReporting.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
    ScrapReporting.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    ScrapReporting.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return ScrapReporting;
};

