#pragma once

#include <string>
#include <vector>
#include <optional>
#include <cstdint>

namespace mosis {

struct ParsedUrl {
    std::string scheme;      // "https"
    std::string host;        // "api.example.com" or "192.0.2.1"
    uint16_t port;           // 443
    std::string path;        // "/api/data"
    std::string query;       // "?key=value"
    bool is_ip_address;      // true if host is IP literal
};

class HttpValidator {
public:
    HttpValidator();

    // Set allowed domains (from app manifest)
    void SetAllowedDomains(const std::vector<std::string>& domains);

    // Clear domain restrictions (for testing)
    void ClearDomainRestrictions();

    // Validate URL
    // Returns parsed URL on success, sets error on failure
    std::optional<ParsedUrl> Validate(const std::string& url, std::string& error);

private:
    std::vector<std::string> m_allowed_domains;
    bool m_domain_restrictions_enabled;

    // IP address validation
    bool IsIPv4Address(const std::string& host);
    bool IsIPv6Address(const std::string& host);
    bool IsPrivateIPv4(const std::string& ip);
    bool IsPrivateIPv6(const std::string& ip);
    bool IsLocalhostIP(const std::string& host);
    bool IsMetadataIP(const std::string& host);
    bool IsBlockedIP(const std::string& host);

    // Domain validation
    bool IsDomainAllowed(const std::string& host);
    bool IsLocalhostName(const std::string& host);
    bool IsMetadataHostname(const std::string& host);

    // URL parsing
    std::optional<ParsedUrl> ParseUrl(const std::string& url);
};

} // namespace mosis
