#ifndef DEVICE_SDK_H
#define DEVICE_SDK_H

#ifdef _WIN32
    #include <windows.h>
    #include <winhttp.h>
#else
    #include <curl/curl.h>
#endif

#include <string>
#include <memory>
#include <ctime>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

struct DeviceInfo {
    std::string uid;
    std::string device_model;
    std::string license_code;  // 可选
};

struct ValidateResponse {
    std::string status;
    std::string license_type;
    std::string expire_time;
    int start_count;
    int max_uses;
    int64_t timestamp;
};

class DeviceClient {
public:
    DeviceClient(const std::string& base_url, const std::string& encrypt_key);
    ~DeviceClient();

    // 设备注册
    bool registerDevice(const DeviceInfo& device_info, std::string& error);
    
    // 设备验证
    bool validateDevice(const std::string& uid, ValidateResponse& response, std::string& error);
    
    // 更新启动次数
    bool updateStartCount(const std::string& uid, int& count, std::string& error);
    
    // 绑定授权码
    bool bindLicense(const std::string& uid, const std::string& license_code, std::string& error);

private:
    std::string base_url_;
    std::string encrypt_key_;
    CURL* curl_;

    static size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp);
    bool post(const std::string& url, const std::string& data, std::string& response, std::string& error);
    bool get(const std::string& url, std::string& response, std::string& error);
    std::string decryptResponse(const std::string& encrypted);
};

#endif // DEVICE_SDK_H 