using System.Diagnostics;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OAuthClient.Models;

namespace OAuthClient.Controllers;

public class HomeController : Controller {
    public IActionResult Index() {
        return View();
    }

    [Authorize]
    public IActionResult AuthCheck() {
        var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;

        ViewData["UserId"] = userId;

        return View();
    }

    [AllowAnonymous]
    public IActionResult Login() {
        return Challenge(new AuthenticationProperties {RedirectUri = "/Home/AuthCheck"}, "OAuth");
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error() {
        return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
    }
}