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

namespace eSuite.Translator;

public interface ITranslationService
{
    Task<string> TranslateAsync(string sourceText, string targetLanguage);
}

public class OllamaTranslationService : ITranslationService
{
    private readonly HttpClient _http;

    public OllamaTranslationService(HttpClient http)
    {
        _http = http;
    }
    
    public async Task<string> TranslateAsync(string sourceText, string targetLanguage)
    {
        Console.Write( $"Translating \"{sourceText}\" into \"{targetLanguage}\": ");
        var request = new
        {
            model = "ZimaBlueAI/HY-MT1.5-1.8:7b",
            prompt = $"Translate this text into {targetLanguage}: {sourceText}",
            stream = false
        };

        var response = await _http.PostAsJsonAsync("/api/generate", request);
        response.EnsureSuccessStatusCode();

        var json = await response.Content.ReadFromJsonAsync<JsonNode>();

        // Ollama returns streaming chunks; for simplicity assume "response" contains the text
        var result = (json?["response"]?.ToString() ?? "").Trim();
        Console.WriteLine(result);

        return result;
    }
}