using Microsoft.AspNetCore.Mvc;
using BankSampahApp.Models;
using BankSampahApp.Services;
using System.Diagnostics;

namespace BankSampahApp.Controllers;

/// <summary>
/// Controller utama untuk halaman Home dengan .NET 9 best practices
/// </summary>
[Route("")]
[Route("[controller]")]
public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;
    private readonly IHomeService _homeService;
    private readonly IAppConfigurationService _appConfig;

    /// <summary>
    /// Constructor dengan dependency injection
    /// </summary>
    /// <param name="logger">Logger untuk logging</param>
    /// <param name="homeService">Service untuk logic bisnis home</param>
    /// <param name="appConfig">Application configuration service</param>
    public HomeController(
        ILogger<HomeController> logger,
        IHomeService homeService,
        IAppConfigurationService appConfig)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        _homeService = homeService ?? throw new ArgumentNullException(nameof(homeService));
        _appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig));
    }

    /// <summary>
    /// Halaman utama aplikasi
    /// </summary>
    /// <returns>View dengan HomeViewModel</returns>
    [HttpGet("")]
    [HttpGet("Index")]
    [ResponseCache(Duration = 300, Location = ResponseCacheLocation.Any, VaryByQueryKeys = new string[] { })]
    public async Task<IActionResult> Index()
    {
        try
        {
            using var scope = _logger.BeginScope(new Dictionary<string, object>
            {
                ["Action"] = nameof(Index),
                ["Controller"] = nameof(HomeController)
            });

            _logger.LogInformation("Loading home page");

            var stopwatch = Stopwatch.StartNew();
            var model = await _homeService.GetHomeViewModelAsync();
            stopwatch.Stop();

            _logger.LogInformation("Home page loaded successfully in {ElapsedMilliseconds}ms", 
                stopwatch.ElapsedMilliseconds);

            // Add performance metrics to ViewBag for debugging
            if (_appConfig.DevelopmentSettings.EnablePerformanceMetrics)
            {
                ViewBag.LoadTime = stopwatch.ElapsedMilliseconds;
            }

            return View(model);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error loading home page");
            
            // Return error view with fallback data
            var fallbackModel = CreateFallbackViewModel();
            return View(fallbackModel);
        }
    }

    /// <summary>
    /// Halaman kebijakan privasi
    /// </summary>
    /// <returns>Privacy view</returns>
    [HttpGet("Privacy")]
    [ResponseCache(Duration = 3600, Location = ResponseCacheLocation.Any)]
    public IActionResult Privacy()
    {
        _logger.LogInformation("Privacy page accessed");
        return View();
    }

    /// <summary>
    /// Halaman error dengan enhanced error handling
    /// </summary>
    /// <returns>Error view dengan ErrorViewModel</returns>
    [HttpGet("Error")]
    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        var requestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
        
        // Get exception details from HttpContext items (set by GlobalExceptionFilter)
        var exception = HttpContext.Items["Exception"] as Exception;
        var storedRequestId = HttpContext.Items["RequestId"] as string;

        _logger.LogWarning("Error page accessed. RequestId: {RequestId}, HasException: {HasException}", 
            requestId, exception != null);

        var model = new ErrorViewModel 
        { 
            RequestId = storedRequestId ?? requestId
        };

        // Add additional debug info in development
        if (_appConfig.ShowDetailedExceptions)
        {
            ViewBag.ExceptionMessage = exception?.Message;
            ViewBag.ExceptionType = exception?.GetType().Name;
        }

        return View(model);
    }

    /// <summary>
    /// API endpoint untuk mendapatkan statistik (untuk AJAX calls)
    /// </summary>
    /// <returns>JSON dengan statistik terkini</returns>
    [HttpGet("api/statistics")]
    [ResponseCache(Duration = 900, Location = ResponseCacheLocation.Any)]
    public async Task<IActionResult> GetStatistics()
    {
        try
        {
            _logger.LogDebug("API statistics endpoint called");

            var statistics = await _homeService.GetStatisticsAsync();
            
            return Json(new
            {
                success = true,
                data = statistics,
                timestamp = DateTime.UtcNow
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error fetching statistics via API");
            
            return Json(new
            {
                success = false,
                error = "Unable to fetch statistics",
                timestamp = DateTime.UtcNow
            });
        }
    }

    /// <summary>
    /// Create fallback view model saat terjadi error
    /// </summary>
    /// <returns>Fallback HomeViewModel</returns>
    private HomeViewModel CreateFallbackViewModel()
    {
        _logger.LogWarning("Creating fallback view model due to error");
        
        return new HomeViewModel
        {
            Title = _appConfig.ApplicationName,
            Subtitle = "Layanan sedang dalam pemeliharaan, silakan coba lagi nanti.",
            Features = new List<FeatureModel>(),
            Statistics = new StatisticsModel
            {
                TotalUsers = 0,
                WasteCollected = 0,
                RewardsDistributed = 0,
                EnvironmentImpact = 0
            }
        };
    }
}