using System.Diagnostics; // Required for Activity and Debugger
using Microsoft.AspNetCore.Mvc; // Provides Controller, IActionResult, etc.
using iyixiGames.Web.Models; // Assumed to contain ErrorViewModel
using Microsoft.AspNetCore.Authorization; // **IMPORTANT: Added for the [Authorize] attribute**
using Microsoft.Extensions.Logging; // Added for ILogger

namespace iyixiGames.Web.Controllers;

/// <summary>
/// Handles requests related to the Home page of the application.
/// </summary>
public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger; // Logger instance for diagnostic messages

    /// <summary>
    /// Constructor for HomeController.
    /// Injects the logger instance.
    /// </summary>
    /// <param name="logger">The logger instance.</param>
    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    /// <summary>
    /// Displays the application's home page (Index page).
    /// This action requires the user to be authenticated. If an unauthenticated user
    /// attempts to access this page, they will be redirected to the configured login path.
    /// </summary>
    [Authorize] // **This attribute ensures that only authenticated users can access this action.**
                // If the user is not logged in, they will be redirected to the LoginPath
                // configured in your CookieAuthenticationOptions in Program.cs (e.g., /Account/Login).
    public IActionResult Index()
    {
        _logger.LogInformation("Accessing Home Index page.");
        return View();
    }

    /// <summary>
    /// Displays the application's privacy policy page.
    /// This action does not require authentication, so anyone can view it.
    /// </summary>
    public IActionResult Privacy()
    {
        _logger.LogInformation("Accessing Privacy page.");
        return View();
    }

    /// <summary>
    /// Displays an error page for unhandled exceptions or specific error scenarios.
    /// The response is configured not to be cached.
    /// </summary>
    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        _logger.LogError("An error occurred. RequestId: {RequestId}", Activity.Current?.Id ?? HttpContext.TraceIdentifier);
        // Creates an ErrorViewModel with the current request ID or a trace identifier.
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}