#pragma once
#include <string>
#include <vector>
#include <filesystem>
#include <nlohmann/json.hpp>

struct ServerConfig {
    std::string url;
    std::string name;
    bool isDefault = false;
    std::string writeToken;
    std::string addedDate;
    
    // JSON serialization
    nlohmann::json toJson() const;
    static ServerConfig fromJson(const nlohmann::json& j);
};

class ServerManager {
public:
    ServerManager();
    
    // Server management
    bool addServer(const std::string& serverUrl, const std::string& writeToken = "");
    bool removeServer(const std::string& serverUrl);
    std::vector<std::string> getServers() const;
    std::string getDefaultServer() const;
    std::string getDefaultPublishServer() const;  // First server with write token
    
    // Token management
    bool setWriteToken(const std::string& serverUrl, const std::string& token);
    std::string getWriteToken(const std::string& serverUrl) const;
    bool hasWriteToken(const std::string& serverUrl) const;
    std::vector<std::string> getServersWithTokens() const;
    
    // Configuration
    bool loadConfiguration();
    bool saveConfiguration();
    void ensureDefaultConfiguration();
    
    // Migration
    bool migrateFromLegacy();
    
private:
    std::vector<ServerConfig> servers_;
    std::filesystem::path configPath_;
    
    bool validateServerUrl(const std::string& url) const;
    bool isServerReachable(const std::string& url) const;
    ServerConfig* findServer(const std::string& url);
    const ServerConfig* findServer(const std::string& url) const;
    std::string getCurrentTimestamp() const;
};