module.exports = (sequelize, DataTypes) => {
  const ProductionNesting = sequelize.define('ProductionNesting', {
    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'
      }
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    production_order_component_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionOrderComponent',
        key: 'id'
      }
    },
    bom_component_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BOMComponent',
        key: 'id'
      }
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    required_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    mrp_unit_weight: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true
    },
    mrp_total_weight: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true
    },
    mrp_weight_uom_code: {
      type: DataTypes.STRING(30),
      allowNull: true
    },
    nesting_confirmed_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    scrap_percentage: {
      type: DataTypes.DECIMAL(7, 3),
      allowNull: false,
      defaultValue: 0
    },
    scrap_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    },
    total_required_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    thickness: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true
    },
    length: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true
    },
    width: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true
    },
    height: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true
    },
    dimension_unit: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'PENDING'
    },
    remarks: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    nesting_file_path: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    nesting_file_name: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    nesting_file_type: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    nesting_file_size: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    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: 'production_nesting',
    indexes: [
      { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
      { fields: ['production_order_id'] },
      { fields: ['production_order_component_id'] },
      { fields: ['material_id'] },
      { fields: ['status'] }
    ]
  });

  ProductionNesting.associate = function(models) {
    ProductionNesting.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionNesting.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ProductionNesting.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    ProductionNesting.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ProductionNesting.belongsTo(models.ProductionOrderComponent, { foreignKey: 'production_order_component_id', as: 'productionOrderComponent' });
    ProductionNesting.belongsTo(models.BOMComponent, { foreignKey: 'bom_component_id', as: 'bomComponent' });
    ProductionNesting.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    ProductionNesting.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    ProductionNesting.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return ProductionNesting;
};
