using System.Net.Http.Json;
using Nexus.Api.Data;

namespace Nexus.Api.Integrations;

public sealed class OllamaProvider(HttpClient client) : IModelProvider
{
    private sealed record OllamaTag(string Name);
    private sealed record OllamaTags(IReadOnlyCollection<OllamaTag>? Models);

    public string Name => "Ollama";

    public async Task<IReadOnlyCollection<ModelProviderStatus>> GetModelsAsync(
        CancellationToken cancellationToken)
    {
        try
        {
            var response = await client.GetFromJsonAsync<OllamaTags>("/api/tags", cancellationToken);
            return response?.Models?
                .Select(model => new ModelProviderStatus(
                    Name,
                    model.Name,
                    OperationalStatus.Online,
                    true,
                    "Local"))
                .ToArray() ?? [];
        }
        catch (Exception exception)
        {
            return
            [
                new(Name, "qwen3:4b", OperationalStatus.Offline, true, exception.Message)
            ];
        }
    }
}
