#pragma once

#include <string>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include "warfactory/ResourceRegistry.h"

using json = nlohmann::json;

namespace warfactory {

/**
 * @brief Represents a meteorite with composition and impact characteristics
 *
 * Meteorites are defined by their resource composition and physical properties
 * that affect impact crater formation and heat generation.
 * Uses ResourceRegistry for fast uint32_t resource ID lookups.
 */
class Meteorite {
private:
    std::string meteorite_type;
    std::unordered_map<uint32_t, uint64_t> resource_composition; // Fast ID-based composition (e15+ scale)
    float mass;
    float impact_energy_multiplier;
    float crater_formation_factor;
    float surface_metal_deposit_ratio;

public:
    Meteorite() = default;
    Meteorite(const json& meteorite_data);

    const std::string& getMeteoriteType() const { return meteorite_type; }
    const std::unordered_map<uint32_t, uint64_t>& getResourceComposition() const { return resource_composition; }

    float getMass() const { return mass; }
    void setMass(float new_mass) { mass = new_mass; }

    // ========================================
    // RESOURCE ACCESS (Performance Optimized)
    // ========================================

    /**
     * @brief Get resource quantity by ID (fast O(1) lookup)
     */
    uint64_t getResourceQuantity(uint32_t resource_id) const;

    /**
     * @brief Get resource quantity by name (slower - uses registry lookup)
     */
    uint64_t getResourceQuantity(const std::string& resource_name) const;

    /**
     * @brief Check if meteorite contains resource by ID (fast)
     */
    bool hasResource(uint32_t resource_id) const;

    /**
     * @brief Check if meteorite contains resource by name (slower)
     */
    bool hasResource(const std::string& resource_name) const;

    /**
     * @brief Add resource to composition by ID
     */
    void addResource(uint32_t resource_id, uint64_t quantity);

    /**
     * @brief Add resource to composition by name
     */
    void addResource(const std::string& resource_name, uint64_t quantity);

    float getImpactEnergyMultiplier() const { return impact_energy_multiplier; }
    float getCraterFormationFactor() const { return crater_formation_factor; }
    float getSurfaceMetalDepositRatio() const { return surface_metal_deposit_ratio; }

    /**
     * @brief Create a copy of this meteorite
     */
    Meteorite copy() const;

    static Meteorite loadFromJson(const std::string& meteorite_type, const json& meteorite_data);
};

} // namespace warfactory