TheInfinityIQ_GCP/src/gcp-backend/GCP.Api/Areas/Identity/Pages/Account/Manage/DeletePersonalData.cshtml.cs
#nullable disable

using System.ComponentModel.DataAnnotations;

using GCP.Api.Data.Entities;

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace GCP.Api.Areas.Identity.Pages.Account.Manage
{
	public class DeletePersonalDataModel : PageModel
	{
		private readonly UserManager<User> _userManager;
		private readonly SignInManager<User> _signInManager;
		private readonly ILogger<DeletePersonalDataModel> _logger;

		public DeletePersonalDataModel(
			UserManager<User> userManager,
			SignInManager<User> signInManager,
			ILogger<DeletePersonalDataModel> logger)
		{
			_userManager = userManager;
			_signInManager = signInManager;
			_logger = logger;
		}

		[BindProperty]
		public InputModel Input { get; set; }

		public class InputModel
		{
			[Required]
			[DataType(DataType.Password)]
			public string Password { get; set; }
		}

		public bool RequirePassword { get; set; }

		public async Task<IActionResult> OnGet()
		{
			var user = await _userManager.GetUserAsync(User);
			if (user is null)
			{
				return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
			}

			RequirePassword = await _userManager.HasPasswordAsync(user);
			return Page();
		}

		public async Task<IActionResult> OnPostAsync()
		{
			var user = await _userManager.GetUserAsync(User);
			if (user is null)
			{
				return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
			}

			RequirePassword = await _userManager.HasPasswordAsync(user);
			if (RequirePassword)
			{
				if (!await _userManager.CheckPasswordAsync(user, Input.Password))
				{
					ModelState.AddModelError(string.Empty, "Incorrect password.");
					return Page();
				}
			}

			var result = await _userManager.DeleteAsync(user);
			var userId = await _userManager.GetUserIdAsync(user);
			if (!result.Succeeded)
			{
				throw new InvalidOperationException($"Unexpected error occurred deleting user.");
			}

			await _signInManager.SignOutAsync();

			_logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId);

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