#pragma once

#include <optional>
#include <vector>

#include <QVector2D>

#include "Tick.h"

#include "entt/entity/entity.hpp"

class EntityAdmin;

// A piece of debris and the scrap amount it still holds (REQ-RES-DEBRIS-DROP).
struct DebrisInfo
{
    entt::entity entity;
    QVector2D position;
    int amount;
};

// Manages debris entities: the salvageable objects dropped by destroyed ships and
// defence stations (REQ-RES-DEBRIS-DROP). Each piece carries a scrap amount that
// salvage modules collect one unit at a time (REQ-SHP-SALVAGE).
class DebrisSystem
{
public:
    explicit DebrisSystem(EntityAdmin& admin);

    entt::entity spawn(QVector2D position, int amount, Tick despawnAt);
    void         tickDespawn(Tick currentTick);

    // Removes the debris and returns its remaining scrap amount, or nullopt if not found.
    std::optional<int> consume(entt::entity entity);

    // Collects a single scrap unit from the debris: decrements its amount by one,
    // destroying the entity once depleted. Returns true if a scrap was collected,
    // false if the entity is invalid or already empty (REQ-SHP-SALVAGE).
    bool collectOne(entt::entity entity);


private:
    EntityAdmin& m_admin;
};

// Debris state read and changed straight off the registry — no system needed.

// Lightweight snapshot for callers that need to iterate all debris.
std::vector<DebrisInfo> getAllDebrisInfo(const EntityAdmin& admin);

// Collects a single scrap unit from the debris: decrements its amount by one,
// destroying the entity once depleted. Returns true if a scrap was collected,
// false if the entity is invalid or already empty (REQ-SHP-SALVAGE).
bool collectOne(EntityAdmin& admin, entt::entity entity);
