using Microsoft.AspNetCore.Mvc;
using SentenceConverterModule.Services.Contracts;

namespace SentenceConverterModule.Controllers
{
    public class HomeController : Controller
    {
        private readonly ISentenceConverterService _converterService;
        private readonly ITextConversionApiService _apiService;

        public HomeController(
            ISentenceConverterService converterService,
            ITextConversionApiService apiService)
        {
            _converterService = converterService;
            _apiService = apiService;
        }

        public IActionResult Index(string language = "pt")
        {
            ViewBag.Language = language;
            ViewBag.Config = _converterService.GetConfiguration(language);

            // Dados para a página de teste
            ViewBag.PageTitle = language switch
            {
                "en" => "Sentence Case Converter - Standalone Test",
                "es" => "Convertidor a Mayúscula Inicial - Prueba Independiente",
                _ => "Conversor para Primeira Maiúscula - Teste Standalone"
            };

            ViewBag.PageDescription = language switch
            {
                "en" => "Test the sentence case converter module independently",
                "es" => "Prueba el módulo convertidor independientemente",
                _ => "Teste o módulo conversor independentemente"
            };

            return View();
        }

        [HttpGet]
        public async Task<IActionResult> HealthCheck()
        {
            try
            {
                var isApiHealthy = await _apiService.IsHealthyAsync();

                return Json(new
                {
                    status = "healthy",
                    service = "sentence-converter-module",
                    apiHealth = isApiHealthy,
                    timestamp = DateTime.UtcNow,
                    version = "1.0.0"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    status = "unhealthy",
                    error = ex.Message,
                    timestamp = DateTime.UtcNow
                });
            }
        }

        [HttpGet]
        public IActionResult TestEndpoints()
        {
            var baseUrl = $"{Request.Scheme}://{Request.Host}";

            var endpoints = new
            {
                module_widget = $"{baseUrl}/modules/sentence-converter",
                footer_message = $"{baseUrl}/modules/footer-message",
                api_convert = $"{baseUrl}/api/converter/convert",
                api_config = $"{baseUrl}/api/converter/config",
                api_health = $"{baseUrl}/api/converter/health",
                home_health = $"{baseUrl}/home/healthcheck"
            };

            return Json(endpoints);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View();
        }
    }
}

