using System;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using GeminiDotnet;
using GeminiDotnet.Extensions.AI;
using Microsoft.Extensions.AI;
using Xunit;

namespace NexusReader.Application.Tests.Services;

public class GeminiEmbeddingTests
{
    [Fact]
    public async Task TestGeminiEmbedding_ModelsAndDimensions()
    {
        var apiKey = Environment.GetEnvironmentVariable("GEMINI_API_KEY");
        if (string.IsNullOrEmpty(apiKey))
        {
            Console.WriteLine("Skipping test: GEMINI_API_KEY is not set.");
            return;
        }

        // Test Model 1: gemini-embedding-001
        try
        {
            var generator = new GeminiEmbeddingGenerator(new GeminiClientOptions
            {
                ApiKey = apiKey,
                ModelId = "gemini-embedding-001"
            });

            // 1. Without dimensions (default)
            var responseDefault = await generator.GenerateAsync(new[] { "Hello world" });
            var vectorDefault = responseDefault.First().Vector.ToArray();
            Console.WriteLine($"[TEST] gemini-embedding-001 default dimensions: {vectorDefault.Length}");

            // 2. With 768 dimensions
            var response768 = await generator.GenerateAsync(new[] { "Hello world" }, new EmbeddingGenerationOptions { Dimensions = 768 });
            var vector768 = response768.First().Vector.ToArray();
            Console.WriteLine($"[TEST] gemini-embedding-001 768 dimensions: {vector768.Length}");

            // 3. With 1536 dimensions
            var response1536 = await generator.GenerateAsync(new[] { "Hello world" }, new EmbeddingGenerationOptions { Dimensions = 1536 });
            var vector1536 = response1536.First().Vector.ToArray();
            Console.WriteLine($"[TEST] gemini-embedding-001 1536 dimensions: {vector1536.Length}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"[TEST] gemini-embedding-001 failed: {ex}");
        }

        // Test Model 2: models/embedding-001
        try
        {
            var generator = new GeminiEmbeddingGenerator(new GeminiClientOptions
            {
                ApiKey = apiKey,
                ModelId = "models/embedding-001"
            });

            var response = await generator.GenerateAsync(new[] { "Hello world" });
            var vector = response.First().Vector.ToArray();
            Console.WriteLine($"[TEST] models/embedding-001 default dimensions: {vector.Length}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"[TEST] models/embedding-001 failed: {ex}");
        }
    }
}
