using Contracts.BindingModels;
using Contracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WebApp.Helpers;

namespace WebApp.Pages
{
    public class SignUpModel : PageModel
    {
        [BindProperty]
        public UserBindingModel UserModel { get; set; }

        public void OnGet()
        {
        }

        public IActionResult OnPostAsync()
        {
            var userRole = APIClient.GetRequest<RoleViewModel>($"role/get?name={Roles.User}");
            if (userRole is null)
            {
                throw new Exception("User role is not found");
            }
            UserModel.Role = new() { Id = userRole.Id };
            var response = APIClient.PostRequest("user/registration", UserModel);

            if (response is null || response is not string)
            {
                throw new Exception("Something wrong LOL!");
            }

            TempData["jwt"] = (string)response;

            return RedirectToPage("TwoFactor");
        }
    }
}