#pragma once

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

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

namespace node669427 {

struct TrackingRecord {
    std::string tracking_id;
    std::string status;
    std::string origin;
    std::string destination;
    std::string service;
    std::string created_at;
    std::string updated_at;
};

class FulfillmentEngine {
public:
    FulfillmentEngine(NodeConfig& config, VaultPtr vault);

    std::string start(const std::string& body);
    std::string track(const std::string& tracking_id);
    std::string updateStatus(const std::string& tracking_id, const std::string& status);

private:
    NodeConfig& config_;
    VaultPtr vault_;

    std::unordered_map<std::string, TrackingRecord> records_;
    std::mutex records_mutex_;
    uint64_t tracking_counter_ = 0;

    std::string generateTrackingId();
    std::string queryCanadaPost(const std::string& tracking_id);

    static size_t curlWriteCb(void* contents, size_t size, size_t nmemb, std::string* userp);
};

} // namespace node669427
