#pragma once

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

#include <string>
#include <unordered_map>
#include <functional>
#include <mutex>
#include <queue>
#include <condition_variable>

namespace node669427 {

enum class SignalType {
    UNKNOWN,
    PAYMENT_INIT,
    PAYMENT_STATUS,
    PAYMENT_CANCEL,
    EMAIL_PURCHASE,
    EMAIL_BULK,
    FULFILLMENT_START,
    SECURITY_EVENT,
    HEALTH_CHECK,
    WAF_UPDATE,
    DNS_ROTATE
};

inline SignalType parseSignal(const std::string& s) {
    if (s == "PAYMENT_INIT") return SignalType::PAYMENT_INIT;
    if (s == "PAYMENT_STATUS") return SignalType::PAYMENT_STATUS;
    if (s == "PAYMENT_CANCEL") return SignalType::PAYMENT_CANCEL;
    if (s == "EMAIL_PURCHASE") return SignalType::EMAIL_PURCHASE;
    if (s == "EMAIL_BULK") return SignalType::EMAIL_BULK;
    if (s == "FULFILLMENT_START") return SignalType::FULFILLMENT_START;
    if (s == "SECURITY_EVENT") return SignalType::SECURITY_EVENT;
    if (s == "HEALTH_CHECK") return SignalType::HEALTH_CHECK;
    if (s == "WAF_UPDATE") return SignalType::WAF_UPDATE;
    if (s == "DNS_ROTATE") return SignalType::DNS_ROTATE;
    return SignalType::UNKNOWN;
}

inline const char* signalName(SignalType s) {
    switch (s) {
        case SignalType::PAYMENT_INIT: return "PAYMENT_INIT";
        case SignalType::PAYMENT_STATUS: return "PAYMENT_STATUS";
        case SignalType::PAYMENT_CANCEL: return "PAYMENT_CANCEL";
        case SignalType::EMAIL_PURCHASE: return "EMAIL_PURCHASE";
        case SignalType::EMAIL_BULK: return "EMAIL_BULK";
        case SignalType::FULFILLMENT_START: return "FULFILLMENT_START";
        case SignalType::SECURITY_EVENT: return "SECURITY_EVENT";
        case SignalType::HEALTH_CHECK: return "HEALTH_CHECK";
        case SignalType::WAF_UPDATE: return "WAF_UPDATE";
        case SignalType::DNS_ROTATE: return "DNS_ROTATE";
        default: return "UNKNOWN";
    }
}

struct Artifact {
    std::string signal_type;
    std::string payload;
    std::string source_node;
    std::string timestamp;
    std::string hmac_signature;
};

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

    std::string process(const std::string& body);
    void run();
    void stop();

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

    std::queue<Artifact> queue_;
    std::mutex queue_mutex_;
    std::condition_variable condition_;
    std::atomic<bool> running_{true};
    uint64_t processed_count_ = 0;
    std::mutex stats_mutex_;

    Artifact parseArtifact(const std::string& body);
    std::string routeSignal(const Artifact& art);

    std::string handlePaymentInit(const Artifact& art);
    std::string handlePaymentStatus(const Artifact& art);
    std::string handlePaymentCancel(const Artifact& art);
    std::string handleEmailPurchase(const Artifact& art);
    std::string handleEmailBulk(const Artifact& art);
    std::string handleFulfillmentStart(const Artifact& art);
    std::string handleSecurityEvent(const Artifact& art);
    std::string handleHealthCheck(const Artifact& art);
    std::string handleWafUpdate(const Artifact& art);
    std::string handleDnsRotate(const Artifact& art);
};

} // namespace node669427
