#pragma once

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#ifndef NOMINMAX
#define NOMINMAX
#endif

#include <cstdint>
#include <cmath>
#include <algorithm>

#include "common/protocol.hpp"
#include "common/math2d.hpp"
#include "memory/game_layout.hpp"

namespace combat {

/**
 * @brief Trạng thái giai đoạn chiến đấu của Boss trong POE2.
 */
enum class BossPhaseState : uint8_t {
    NoBoss = 0,             // Không có Boss trong radar
    ActiveCombat = 1,       // Boss xuất hiện và có thể bị tấn công (Vulnerable / Targetable)
    ImmunityPhase = 2,      // Boss bất tử (Bit 8 kEntityFlagInvulnerable hoặc untargetable)
    DisappearedPhase = 3    // Boss lặn/bay biến mất khỏi radar (chuyển giai đoạn)
};

/**
 * @brief Cấu hình điều phối giai đoạn Boss & Quỹ đạo né đòn Orbital Kiting.
 */
struct BossPhaseConfig {
    float defaultOrbitalRadius = 180.0f;        // Bán kính quỹ đạo né chiêu diện rộng (units)
    float minOrbitalRadius = 120.0f;            // Bán kính tối thiểu
    float maxOrbitalRadius = 260.0f;            // Bán kính tối đa
    bool clockwiseOrbit = true;                 // Hướng quay quỹ đạo (thuận chiều kim đồng hồ)
    uint32_t phaseTimeoutMs = 20000;            // Hạn định pha bất tử tối đa (20s)
    float radialCorrectionGain = 0.02f;         // Hệ số hiệu chỉnh lực hút/đẩy về bán kính chuẩn
};

/**
 * @brief Thông tin giai đoạn Boss hiện tại.
 * POD 64 bytes (1 cache line), 0 dynamic allocation.
 */
struct alignas(64) BossPhaseInfo {
    BossPhaseState state = BossPhaseState::NoBoss;
    uint32_t bossId = 0;
    float bossLastPosX = 0.0f;
    float bossLastPosY = 0.0f;
    float arenaCenterX = 0.0f;
    float arenaCenterY = 0.0f;
    uint64_t phaseStartTimestampMs = 0;
    bool isInvulnerable = false;
    bool isTargetable = true;
    bool isBossOnRadar = false;
    bool hasSeenTargetable = false;
};

/**
 * @brief Vector tiếp tuyến quỹ đạo né chiêu diện rộng (Orbital Kiting).
 */
struct OrbitalVector {
    float moveDx = 0.0f;
    float moveDy = 0.0f;
    float currentRadius = 0.0f;
    float currentAngleRad = 0.0f;
};

/**
 * @brief BossPhaseController - Quản lý giai đoạn bất tử và né chiêu diện rộng của Boss.
 * 
 * Mục tiêu Hot Path (120Hz):
 * 1. Nhận diện trạng thái bất tử (Immunity / Untargetable / Disappeared) của Boss tức thời.
 * 2. Ngắt xả Heavy Finisher và Stagger Spender khi Boss bất tử để tránh lãng phí tài nguyên.
 * 3. Sinh vector di chuyển Orbital Kiting Pattern vòng tròn quanh tâm sàn đấu để né AoE / Bullet Hell.
 * 4. Tự động phục hồi trạng thái tấn công Auto-Facing & Stutter-Stepping khi hết bất tử.
 * 5. Data-Oriented Design: O(1) memory, 0 dynamic allocation, latency < 0.05 µs.
 */
class BossPhaseController {
public:
    explicit BossPhaseController(const BossPhaseConfig& config = BossPhaseConfig{});

    void SetConfig(const BossPhaseConfig& config) { m_config = config; }
    const BossPhaseConfig& Config() const { return m_config; }
    const BossPhaseInfo& PhaseInfo() const { return m_info; }

    /**
     * @brief Đánh giá và cập nhật trạng thái giai đoạn Boss từ Telemetry.
     * O(N), 0 dynamic allocation, latency < 0.02 µs.
     * 
     * @param packet Gói Telemetry thế giới
     * @param activeBossId ID của Boss đang giao chiến (nếu có)
     * @param nowMs Mốc thời gian hiện tại (ms)
     * @return BossPhaseState Trạng thái phase mới
     */
    BossPhaseState UpdatePhase(const TelemetryPacket& packet,
                               uint32_t activeBossId,
                               uint64_t nowMs);

    /**
     * @brief Kiểm tra xem Boss hiện có đang ở trạng thái bất tử / miễn nhiễm không.
     */
    bool IsBossInvulnerable() const {
        return m_info.state == BossPhaseState::ImmunityPhase ||
               m_info.state == BossPhaseState::DisappearedPhase;
    }

    /**
     * @brief Tính toán vector tiếp tuyến quỹ đạo tròn (Orbital Kiting Pattern) quanh tâm sàn đấu.
     * 0 dynamic allocation, thực thi trong < 15 ns.
     * 
     * @param playerX Tọa độ X người chơi
     * @param playerY Tọa độ Y người chơi
     * @return OrbitalVector Vector di chuyển chuẩn hóa (moveDx, moveDy)
     */
    OrbitalVector ComputeOrbitalKiteVector(float playerX, float playerY) const;

    /**
     * @brief Thiết lập thủ công tọa độ tâm sàn đấu.
     */
    void SetArenaCenter(float centerX, float centerY);

    /**
     * @brief Reset controller về trạng thái NoBoss ban đầu.
     */
    void Reset();

private:
    BossPhaseConfig m_config;
    BossPhaseInfo m_info;
    bool m_hasExplicitCenter = false;
};

} // namespace combat
