using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

public class DocumentResponse
{
    [JsonPropertyName("documents")]  // Konsistent mit Such-Endpoint
    public List<SearchResult> Documents { get; set; }
}

public class PdfServiceClient
{
    private readonly HttpClient _httpClient;
    private const string BaseUrl = "http://localhost:8000"; // Microservice-URL

    public PdfServiceClient()
    {
        _httpClient = new HttpClient
        {
            BaseAddress = new Uri(BaseUrl), Timeout = TimeSpan.FromSeconds(30)
        };
    }

    public async Task<string> GetDocumentContentAsync(int documentId)
    {
        var response = await _httpClient.GetAsync($"{BaseUrl}/documents/{documentId}/markdown");
        response.EnsureSuccessStatusCode();
        var json = await response.Content.ReadAsStringAsync();
        var result = JsonSerializer.Deserialize<JsonElement>(json);
        return result.GetProperty("content").GetString();
    }

    public async Task<List<SearchResult>> SearchDocumentsAsync(string query)
    {
        var response = await _httpClient.GetAsync($"{BaseUrl}/documents/search?query={query}");
        response.EnsureSuccessStatusCode();
        var json = await response.Content.ReadAsStringAsync();
        return JsonSerializer.Deserialize<List<SearchResult>>(json);
    }
}

public class SearchResult
{
    [JsonPropertyName("id")]
    public int Id { get; set; }

    [JsonPropertyName("filename")]
    public string Filename { get; set; }
    [JsonPropertyName("content")]
    public string Content { get; set; }
}