using System.Net.Http.Headers;
using System.Text.Json;

Console.WriteLine("=== RealCV API Integration Tester ===\n");

// Test 1: FCA Register API
Console.WriteLine("1. Testing FCA Register API...");
try
{
    var fcaClient = new HttpClient();
    fcaClient.BaseAddress = new Uri("https://register.fca.org.uk/services/V0.1/");
    fcaClient.DefaultRequestHeaders.Add("X-Auth-Email", "peter.foster@ukdataservices.co.uk");
    fcaClient.DefaultRequestHeaders.Add("X-Auth-Key", "9ae1aee51e5c717a1135775501c89075");

    var fcaResponse = await fcaClient.GetAsync("Individuals?q=John%20Smith&page=1");
    Console.WriteLine($"   Status: {fcaResponse.StatusCode}");

    if (fcaResponse.IsSuccessStatusCode)
    {
        var content = await fcaResponse.Content.ReadAsStringAsync();
        Console.WriteLine($"   ✓ FCA API working");
        using var doc = JsonDocument.Parse(content);
        if (doc.RootElement.TryGetProperty("Data", out var data) && data.ValueKind == JsonValueKind.Array)
        {
            Console.WriteLine($"   Found {data.GetArrayLength()} individuals matching 'John Smith'");
        }
        else
        {
            Console.WriteLine($"   Response: {content.Substring(0, Math.Min(200, content.Length))}");
        }
    }
    else
    {
        Console.WriteLine($"   ✗ Error: {fcaResponse.StatusCode}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"   ✗ Error: {ex.Message}");
}

Console.WriteLine();

// Test 2: ORCID API
Console.WriteLine("2. Testing ORCID API...");
try
{
    var orcidClient = new HttpClient();
    orcidClient.BaseAddress = new Uri("https://pub.orcid.org/v3.0/");
    orcidClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // Get a known ORCID record directly
    var orcidResponse = await orcidClient.GetAsync("0000-0001-5109-3700/record");
    Console.WriteLine($"   Status: {orcidResponse.StatusCode}");

    if (orcidResponse.IsSuccessStatusCode)
    {
        var content = await orcidResponse.Content.ReadAsStringAsync();
        Console.WriteLine($"   ✓ ORCID API working");
        using var doc = JsonDocument.Parse(content);
        if (doc.RootElement.TryGetProperty("person", out var person) &&
            person.TryGetProperty("name", out var name))
        {
            var givenName = "";
            var familyName = "";
            if (name.TryGetProperty("given-names", out var gn) && gn.TryGetProperty("value", out var gnv))
                givenName = gnv.GetString() ?? "";
            if (name.TryGetProperty("family-name", out var fn) && fn.TryGetProperty("value", out var fnv))
                familyName = fnv.GetString() ?? "";
            Console.WriteLine($"   Retrieved record for: {givenName} {familyName}");
        }
    }
    else
    {
        Console.WriteLine($"   ✗ Error: {orcidResponse.StatusCode}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"   ✗ Error: {ex.Message}");
}

Console.WriteLine();

// Test 3: GitHub API
Console.WriteLine("3. Testing GitHub API...");
try
{
    var githubClient = new HttpClient();
    githubClient.BaseAddress = new Uri("https://api.github.com/");
    githubClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
    githubClient.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2022-11-28");
    githubClient.DefaultRequestHeaders.UserAgent.ParseAdd("RealCV/1.0");

    var githubResponse = await githubClient.GetAsync("users/torvalds");
    Console.WriteLine($"   Status: {githubResponse.StatusCode}");

    if (githubResponse.IsSuccessStatusCode)
    {
        var content = await githubResponse.Content.ReadAsStringAsync();
        Console.WriteLine($"   ✓ GitHub API working");
        using var doc = JsonDocument.Parse(content);
        var name = doc.RootElement.GetProperty("name").GetString();
        var repos = doc.RootElement.GetProperty("public_repos").GetInt32();
        var followers = doc.RootElement.GetProperty("followers").GetInt32();
        Console.WriteLine($"   User: {name}, Repos: {repos}, Followers: {followers}");
    }
    else
    {
        Console.WriteLine($"   ✗ Error: {githubResponse.StatusCode}");
    }

    if (githubResponse.Headers.TryGetValues("X-RateLimit-Remaining", out var remaining))
    {
        Console.WriteLine($"   Rate limit remaining: {remaining.First()}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"   ✗ Error: {ex.Message}");
}

Console.WriteLine("\n=== Tests complete ===");
