using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using OnlyOneAccessTemplate.Services.OnlyOneAccessTemplate.Services; using OnlyOneAccessTemplate.Services; using OnlyOneAccessTemplate.Models; namespace OnlyOneAccessTemplate.Controllers { [ApiController] [Route("converter")] public class ConverterController : BaseController { private readonly IServiceProvider _serviceProvider; //private readonly ILogger _logger; private readonly Dictionary _converterTypes; public ConverterController( ISiteConfigurationService siteConfig, ILanguageService languageService, ISeoService seoService, IMemoryCache cache, IConfiguration configuration, IServiceProvider serviceProvider //,ILogger logger ) : base(siteConfig, languageService, seoService, cache, configuration) { _serviceProvider = serviceProvider; //this._logger = logger; // Registrar tipos de conversores disponíveis _converterTypes = new Dictionary { ["text-case"] = typeof(TextCaseConverterService), ["csv-json"] = typeof(CsvToJsonConverterService), ["image-ocr"] = typeof(ImageToTextConverterService), ["sentence-converter"] = typeof(UpperLowerConversorService) // Adicionar novos conversores aqui }; } [HttpPost("api/convert/{converterType}")] public async Task Convert(string converterType, [FromForm] ConversionRequestDto requestDto) { try { if (!_converterTypes.ContainsKey(converterType)) { return BadRequest(new { success = false, message = "Conversor não encontrado" }); } var converterService = (IConverterService)_serviceProvider.GetRequiredService(_converterTypes[converterType]); var request = new ConversionRequest( Language: requestDto.Language ?? GetCurrentLanguage(), InputType: requestDto.InputType, TextInput: requestDto.TextInput, FileInput: requestDto.FileInput, UrlInput: requestDto.UrlInput, Options: requestDto.Options ); // Validar entrada if (!await converterService.ValidateInputAsync(request)) { return BadRequest(new { success = false, message = "Entrada inválida" }); } // Executar conversão var result = await converterService.ConvertAsync(request); if (!result.Success) { return BadRequest(new { success = false, message = result.ErrorMessage }); } // Retornar resultado baseado no tipo de saída if (!string.IsNullOrEmpty(result.OutputText)) { return Ok(new { success = true, outputText = result.OutputText, metadata = result.Metadata }); } else if (result.OutputFile != null) { return File(result.OutputFile, result.OutputMimeType ?? "application/octet-stream", result.OutputFileName ?? "converted_file"); } else { return Ok(new { success = true, previewHtml = result.PreviewHtml, metadata = result.Metadata }); } } catch (Exception ex) { //_logger.LogError(ex, "Erro na conversão {ConverterType}", converterType); return StatusCode(500, new { success = false, message = "Erro interno do servidor" }); } } [HttpGet("config/{converterType}")] public IActionResult GetConverterConfig(string converterType) { try { if (!_converterTypes.ContainsKey(converterType)) { return NotFound(new { success = false, message = "Conversor não encontrado" }); } var converterService = (IConverterService)_serviceProvider.GetRequiredService(_converterTypes[converterType]); var language = GetCurrentLanguage(); var config = converterService.GetConfiguration(language); return Ok(new { success = true, config }); } catch (Exception ex) { //_logger.LogError(ex, "Erro ao buscar configuração do conversor {ConverterType}", converterType); return StatusCode(500, new { success = false, message = "Erro interno do servidor" }); } } [HttpGet("test")] public IActionResult Test() { return Ok(new { message = "Controller funcionando", timestamp = DateTime.Now }); } } }