using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mindforge.API.Models.Enums;
using Mindforge.API.Providers;
using Mindforge.API.Services.Interfaces;

namespace Mindforge.API.Services
{
    public class AgentService : IAgentService
    {
        private readonly IEnumerable<ILlmApiProvider> _providers;

        public AgentService(IEnumerable<ILlmApiProvider> providers)
        {
            _providers = providers;
        }

        public Task<string> ProcessRequestAsync(LlmProvider providerEnum, string systemPrompt, string userPrompt, string model)
        {
            ILlmApiProvider provider = providerEnum switch
            {
                LlmProvider.OpenAI => _providers.OfType<OpenAIApiProvider>().FirstOrDefault()
                                      ?? throw new Exception("OpenAI provider not registered"),
                LlmProvider.Gemini => _providers.OfType<GeminiApiProvider>().FirstOrDefault()
                                      ?? throw new Exception("Gemini provider not registered"),
                _ => throw new Exception("Unknown provider")
            };

            return provider.SendRequestAsync(systemPrompt, userPrompt, model);
        }

        public Task<string> ProcessRequestBatchAsync(LlmProvider providerEnum, string systemPrompt, string userPrompt, string model)
        {
            ILlmApiProvider provider = providerEnum switch
            {
                LlmProvider.OpenAI => _providers.OfType<OpenAIApiProvider>().FirstOrDefault()
                                      ?? throw new Exception("OpenAI provider not registered"),
                LlmProvider.Gemini => _providers.OfType<GeminiApiProvider>().FirstOrDefault()
                                      ?? throw new Exception("Gemini provider not registered"),
                _ => throw new Exception("Unknown provider")
            };

            return provider.SendRequestBatchAsync(systemPrompt, userPrompt, model);
        }
    }
}
