#pragma once

#include <filesystem>
#include <string>
#include <string_view>

namespace poe2::platform {

inline constexpr std::string_view DEFAULT_OFFSETS_URL =
    "https://raw.githubusercontent.com/mirsixabcf2-commits/Poe2VisualTool/main/offsets.toml";

struct SyncResult {
    bool success = false;
    bool updated = false;
    std::string message;
    std::string remoteVersion;
    std::string localVersion;
};

class CloudOffsetsSync {
public:
    explicit CloudOffsetsSync(
        std::filesystem::path localPath = "offsets.toml",
        std::string remoteUrl = std::string(DEFAULT_OFFSETS_URL),
        int timeoutMs = 3500
    );

    /// @brief Trích xuất chuỗi phiên bản hoặc dấu thời gian từ nội dung TOML
    static std::string ExtractVersionOrTimestamp(std::string_view tomlContent);

    /// @brief Tải nội dung TOML từ máy chủ GitHub Raw qua giao thức HTTPS WinHTTP
    std::pair<bool, std::string> FetchRemote(std::string_view overrideUrl = "");

    /// @brief Kiểm tra và cập nhật tệp offsets.toml nếu phiên bản remote mới hơn
    SyncResult CheckAndUpdate();

    const std::filesystem::path& GetLocalPath() const noexcept { return localPath_; }
    const std::string& GetRemoteUrl() const noexcept { return remoteUrl_; }

private:
    std::filesystem::path localPath_;
    std::string remoteUrl_;
    int timeoutMs_;
};

} // namespace poe2::platform
