#pragma once
#include <cstdint>
#include <mutex>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "Core/Models.h"

// -------------------------------------------------------------------------
// BulletTrack — full history for one bullet address.
// -------------------------------------------------------------------------

struct BulletTrack {
    uint64_t address      = 0;

    int64_t firstSeenMs   = 0;
    int64_t lastSeenMs    = 0;
    int64_t finalizedMs   = 0;   // 0 = not yet finalised

    Vector3 currentPos;
    Vector3 initialPos;
    Vector3 finalPos;

    Vector3 initialDir;
    Vector3 lastDir;

    float   speedMps      = 0.0f;

    bool    isActive      = false;  // seen in the most-recent update
    bool    isPhantom     = false;  // no longer in live list
    bool    isCompleted   = false;  // came to rest

    // (timestamp_ms, world-position) pairs, oldest first.
    std::vector<std::pair<int64_t, Vector3>> points;
};

// -------------------------------------------------------------------------
// BulletTrackCache
// -------------------------------------------------------------------------

class BulletTrackCache {
public:
    /// Merge a fresh bullet snapshot into the cache.
    void Update(const std::vector<DayZBulletEntry>& bullets, int64_t nowMs);

    /// Thread-safe snapshot of all currently tracked bullets.
    std::vector<BulletTrack> GetSnapshot() const;

private:
    mutable std::mutex                        m_mutex;
    std::unordered_map<uint64_t, BulletTrack> m_tracks;

    // A bullet that hasn't moved more than kStillM over kStillMs is
    // considered stopped and its track is finalised.
    static constexpr int64_t kStillMs          = 75;
    static constexpr float   kStillM           = 0.05f;

    // How long to retain a completed track after it was finalised.
    static constexpr int64_t kRetentionMs      = 1500;

    // How long to keep a phantom (disappeared but not completed) track.
    static constexpr int64_t kPhantomLifetimeMs = 5000;
};
