using SentenceConverterModule.Models;
using SentenceConverterModule.Services.Contracts;

namespace SentenceConverterModule.Services
{
    public class SentenceConverterService : ISentenceConverterService
    {
        private readonly ITextConversionApiService _apiService;
        private readonly ILogger<SentenceConverterService> _logger;

        public SentenceConverterService(
            ITextConversionApiService apiService,
            ILogger<SentenceConverterService> logger)
        {
            _apiService = apiService;
            _logger = logger;
        }

        public SentenceConverterService()
        {
        }

        public async Task<ConversionResult> ConvertAsync(ConversionRequest request)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(request.TextInput))
                {
                    return new ConversionResult(false, ErrorMessage: "Texto não pode estar vazio");
                }

                var resultado = await _apiService.ConvertToSentenceCaseAsync(request.TextInput);

                return new ConversionResult(
                    Success: true,
                    OutputText: resultado,
                    Metadata: new Dictionary<string, object>
                    {
                        ["originalLength"] = request.TextInput.Length,
                        ["convertedLength"] = resultado.Length,
                        ["processedAt"] = DateTime.UtcNow
                    }
                );
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Erro na conversão para sentence case");
                return new ConversionResult(false, ErrorMessage: "Erro ao processar conversão");
            }
        }

        public async Task<bool> ValidateInputAsync(ConversionRequest request)
        {
            if (string.IsNullOrWhiteSpace(request.TextInput))
                return false;

            if (request.TextInput.Length > 10000) // Limite de 10k caracteres
                return false;

            return await Task.FromResult(true);
        }

        public ConverterConfiguration GetConfiguration(string language)
        {
            var texts = GetLocalizedTexts(language);

            return new ConverterConfiguration
            {
                ConverterType = "text",
                OutputType = "text",
                HasAdvancedOptions = false,
                AllowShare = true,
                LocalizedTexts = texts
            };
        }

        private Dictionary<string, string> GetLocalizedTexts(string language)
        {
            return language switch
            {
                "en" => new Dictionary<string, string>
                {
                    ["ConverterTitle"] = "Convert to Sentence Case",
                    ["ConverterDescription"] = "Convert your text to sentence case format",
                    ["InputPlaceholder"] = "Enter your text here...",
                    ["OutputLabel"] = "Converted Text:",
                    ["ConvertButton"] = "Convert Text",
                    ["CopyButton"] = "Copy Result",
                    ["ClearButton"] = "Clear",
                    ["Step1Title"] = "Type",
                    ["Step1Description"] = "Enter your text",
                    ["Step2Title"] = "Convert",
                    ["Step2Description"] = "Click to convert",
                    ["Step3Title"] = "Copy",
                    ["Step3Description"] = "Copy the result"
                },
                "es" => new Dictionary<string, string>
                {
                    ["ConverterTitle"] = "Convertir a Mayúscula Inicial",
                    ["ConverterDescription"] = "Convierte tu texto al formato de mayúscula inicial",
                    ["InputPlaceholder"] = "Ingresa tu texto aquí...",
                    ["OutputLabel"] = "Texto Convertido:",
                    ["ConvertButton"] = "Convertir Texto",
                    ["CopyButton"] = "Copiar Resultado",
                    ["ClearButton"] = "Limpiar",
                    ["Step1Title"] = "Escribir",
                    ["Step1Description"] = "Ingresa tu texto",
                    ["Step2Title"] = "Convertir",
                    ["Step2Description"] = "Haz clic para convertir",
                    ["Step3Title"] = "Copiar",
                    ["Step3Description"] = "Copia el resultado"
                },
                _ => new Dictionary<string, string>
                {
                    ["ConverterTitle"] = "Converter para Primeira Maiúscula",
                    ["ConverterDescription"] = "Converte seu texto para o formato de primeira letra maiúscula",
                    ["InputPlaceholder"] = "Digite seu texto aqui...",
                    ["OutputLabel"] = "Texto Convertido:",
                    ["ConvertButton"] = "Converter Texto",
                    ["CopyButton"] = "Copiar Resultado",
                    ["ClearButton"] = "Limpar",
                    ["Step1Title"] = "Digite",
                    ["Step1Description"] = "Digite seu texto",
                    ["Step2Title"] = "Converter",
                    ["Step2Description"] = "Clique para converter",
                    ["Step3Title"] = "Copiar",
                    ["Step3Description"] = "Copie o resultado"
                }
            };
        }
    }
}
