#if !NET8
using System;
using System.Web.Mvc;
#endif

#if NET8
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
#endif

using Fab2ApprovalSystem.Misc;

namespace Fab2ApprovalSystem.Controllers;

[Authorize]
#if !NET8
[SessionExpireFilter]
#endif
#if NET8
[Route("[controller]")]
#endif
public class PCRBController : Controller {
    public ActionResult Edit(int issueID) {
        string jwt = GlobalVars.GetJWT(GetSession());
        string encodedJwt = System.Net.WebUtility.UrlEncode(jwt);
        string refreshToken = GlobalVars.GetRefreshToken(GetSession());
        string encodedRefreshToken = System.Net.WebUtility.UrlEncode(refreshToken);
        string mrbUrl = $"{GlobalVars.AppSettings.WasmClientUrl}/redirect?jwt={encodedJwt}&refreshToken={encodedRefreshToken}&redirectPath=/pcrb/{issueID}";

        return Redirect(mrbUrl);
    }

#if !NET8

    private System.Web.HttpSessionStateBase GetSession() =>
        Session;

    private JsonResult GetJsonResult(object? data) =>
        Json(data, JsonRequestBehavior.AllowGet);

    private bool IsAjaxRequest() =>
        Request.IsAjaxRequest();

#endif

#if NET8

    private Microsoft.AspNetCore.Http.ISession GetSession() =>
        HttpContext.Session;

    private JsonResult GetJsonResult(object? data) =>
        Json(data);

    private bool IsAjaxRequest() =>
        Request.Headers.TryGetValue("X-Requested-With", out Microsoft.Extensions.Primitives.StringValues strings) && strings[0] == "XMLHttpRequest";

#endif

    private string GetUserIdentityName() =>
        @User.Identity.Name;

}