#pragma once

#include "engine_config.h"
#include "engine_crypto.h"
#include "engine_vault.h"
#include "engine_security.h"
#include "engine_payment.h"
#include "engine_fulfillment.h"
#include "engine_email.h"
#include "engine_artifact.h"

#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <functional>
#include <queue>

namespace node669427 {

struct HttpRequest {
    std::string method;
    std::string path;
    std::string body;
    std::unordered_map<std::string, std::string> headers;
    std::string client_ip;
};

class ThreadPool {
public:
    explicit ThreadPool(size_t threads);
    ~ThreadPool();
    void enqueue(std::function<void()> task);
    void stop();

private:
    std::vector<std::thread> workers_;
    std::queue<std::function<void()>> tasks_;
    std::mutex queue_mutex_;
    std::condition_variable condition_;
    std::atomic<bool> running_{true};
};

class HttpServer {
public:
    HttpServer(NodeConfig& config, CryptoEngine& crypto, VaultPtr vault,
               SecurityEngine& security, ArtifactProcessor& artifacts,
               PaymentProcessor& payments, EmailDispatcher& email,
               FulfillmentEngine& fulfillment);
    ~HttpServer();

    bool start();
    void stop();
    void run();

private:
    NodeConfig& config_;
    CryptoEngine& crypto_;
    VaultPtr vault_;
    SecurityEngine& security_;
    ArtifactProcessor& artifacts_;
    PaymentProcessor& payments_;
    EmailDispatcher& email_;
    FulfillmentEngine& fulfillment_;

    std::unique_ptr<ThreadPool> thread_pool_;
    int server_fd_ = -1;
    std::atomic<bool> running_{false};
    
    std::unordered_set<std::string> seen_nonces_;
    std::mutex nonce_mutex_;

    void handle_client(int client_fd, const std::string& client_ip);
    std::string route_request(const HttpRequest& req);
    bool validate_hmac(const HttpRequest& req);

    std::string handle_artifact(const HttpRequest& req);
    std::string handle_health(const HttpRequest& req);
    std::string handle_status(const HttpRequest& req);
    std::string handle_payment_init(const HttpRequest& req);
    std::string handle_payment_status(const HttpRequest& req);
    std::string handle_email_send(const HttpRequest& req);
    std::string handle_fulfillment_start(const HttpRequest& req);
    std::string handle_security_events(const HttpRequest& req);
    std::string handle_ai_infer(const HttpRequest& req);
    std::string handle_vault_key(const HttpRequest& req);

    static std::string parse_method(const std::string& request);
    static std::string parse_path(const std::string& request);
    static std::string parse_body(const std::string& request);
    static std::unordered_map<std::string, std::string> parse_headers(const std::string& request);
    static std::string extract_path_param(const std::string& path, size_t segment);
    static void send_response(int client_fd, int status, const std::string& body, const std::unordered_map<std::string, std::string>& extra_headers = {});
};

} // namespace node669427
