using System.Diagnostics;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace JobsMedical.Web.Pages;

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
    public string? RequestId { get; set; }

    public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

    /// <summary>The real exception — shown ONLY to a logged-in Admin, so production 500s can be
    /// diagnosed without server-log access. Hidden from everyone else.</summary>
    public string? AdminDetail { get; private set; }
    public string? AdminPath { get; private set; }

    public void OnGet()
    {
        RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;

        if (User.IsInRole("Admin"))
        {
            var feat = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
            AdminPath = feat?.Path;
            if (feat?.Error is { } ex)
                AdminDetail = ex.GetType().FullName + ": " + ex.Message
                    + (ex.InnerException is { } ie ? $"\n  ↳ {ie.GetType().Name}: {ie.Message}" : "")
                    + "\n\n" + ex.StackTrace;
        }
    }
}

