#pragma once

#include "engine_config.h"
#include "engine_vault.h"
#include "engine_crypto.h"

#include <string>
#include <unordered_map>
#include <mutex>
#include <chrono>

namespace node669427 {

enum class PaymentMethod {
    UNKNOWN,
    STRIPE,
    XMR,
    USDT_TRC,
    USDC_POLY,
    ETRANSFER
};

inline PaymentMethod parseMethod(const std::string& s) {
    if (s == "stripe") return PaymentMethod::STRIPE;
    if (s == "xmr") return PaymentMethod::XMR;
    if (s == "usdt-trc") return PaymentMethod::USDT_TRC;
    if (s == "usdc-poly") return PaymentMethod::USDC_POLY;
    if (s == "etransfer") return PaymentMethod::ETRANSFER;
    return PaymentMethod::UNKNOWN;
}

struct PaymentRecord {
    std::string id;
    PaymentMethod method;
    std::string status;
    double amount;
    std::string currency;
    std::string customer_id;
    std::string metadata;
    std::chrono::system_clock::time_point created;
    std::chrono::system_clock::time_point updated;
};

class PaymentProcessor {
public:
    PaymentProcessor(NodeConfig& config, VaultPtr vault, CryptoEngine& crypto);

    std::string initiate(const std::string& body);
    std::string status(const std::string& payment_id);
    std::string cancel(const std::string& payment_id);

private:
    NodeConfig& config_;
    VaultPtr vault_;
    CryptoEngine& crypto_;


    std::string handleStripe(const std::string& payload);
    std::string handleXMR(const std::string& payload);
    std::string handleUSDTTRC(const std::string& payload);
    std::string handleUSDCPoly(const std::string& payload);
    std::string handleETransfer(const std::string& payload);

    std::string generatePaymentId();
    static size_t curlWriteCb(void* contents, size_t size, size_t nmemb, std::string* userp);
    std::string curlPost(const std::string& url, const std::string& body,
                         const std::vector<std::string>& extra_headers);
};

} // namespace node669427
