using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;

namespace WebApp.Authentication
{
    public class AuthController : Controller
    {
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> CookieLogin(string email, string password)
        {
            // Based on: https://www.codeproject.com/articles/Understanding-authentication-in-Blazor-and-ASP-NET
            // TODO: Fix this up
            // Generate the claims
            var claims = new List<Claim>();
            claims.Add(new Claim(ClaimTypes.Name, "John Patton"));
            claims.Add(new Claim(ClaimTypes.Role, "Contributor"));

            var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, "Auth"));

            await HttpContext.SignInAsync("Auth", principal).ConfigureAwait(false);

            return Redirect("/");
        }

        [HttpPost]
        public async Task<IActionResult> CookieLogout()
        {
            await HttpContext.SignOutAsync("Auth").ConfigureAwait(false);

            return Redirect("/login");
        }
    }
}
