using System.ComponentModel.DataAnnotations;
using EnotaryoPH.Data;
using EnotaryoPH.Data.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace EnotaryoPH.Web.Pages
{
    public class RegisterModel : PageModel
    {
        private readonly NotaryoDBContext _notaryoDBContext;
        private readonly IPasswordService _passwordService;

        public RegisterModel(NotaryoDBContext notaryoDBContext, IPasswordService passwordService)
        {
            _notaryoDBContext = notaryoDBContext;
            _passwordService = passwordService;
        }

        public void OnGet() => RoleType = "Principal";

        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            var hasDuplicateEmail = _notaryoDBContext.Users.Any(u => EF.Functions.Like(u.Email, Email));
            if (hasDuplicateEmail)
            {
                ModelState.AddModelError(nameof(Email), "That email already exists in the database.");
                return Page();
            }

            var today = DateTime.Now;
            if (today.Year - BirthDate.Year < 18 || (today.Year - BirthDate.Year == 18 && BirthDate.Month < today.Month))
            {
                ModelState.AddModelError(nameof(BirthDate), "You must be 18 or older");
                return Page();
            }

            var newUser = new User
            {
                BirthDate = new DateTime(BirthDate.Ticks, DateTimeKind.Utc),
                Email = Email,
                PasswordHash = _passwordService.HashPassword(Password),
                PhoneNumber = PhoneNumber,
                Role = RoleType,
                User_UID = Guid.CreateVersion7(DateTime.UtcNow)
            };
            _notaryoDBContext.Users.Add(newUser);

            if (RoleType == "Notary Public")
            {
                var newLawyer = new Lawyer
                {
                    User = newUser,
                    CommissionExpiration = new DateTime(CommissionExpiration.Value.Ticks, DateTimeKind.Utc),
                    CommissionNumber = CommissionNumber,
                    IBPNumber = IBPNumber,
                    MCLEComplianceNumber = MCLEComplianceNumber,
                    MCLEDate = MCLEDate.ToUTC(),
                    OfficeAddress = OfficeAddress,
                    PTRDate = PTRDate.ToUTC(),
                    PTRlocation = PTRLocation,
                    PTRNumber = PTRNumber,
                    Rollnumber = RollNumber,
                    Status = "New",
                    CreatedOn = DateTime.UtcNow,
                    Lawyer_UID = Guid.CreateVersion7(DateTime.UtcNow)
                };
                _notaryoDBContext.Lawyers.Add(newLawyer);
            }

            _notaryoDBContext.SaveChanges();

            return RoleType == "Principal"
                ? Redirect("/Principal/Dashboard/Dashboard")
                : Redirect("/Notary/Dashboard/Dashboard");
        }

        [BindProperty]
        public string Email { get; set; }

        [BindProperty]
        public string Password { get; set; }

        [BindProperty]
        [Compare(nameof(Password), ErrorMessage = "Passwords must match")]
        public string ConfirmPassword { get; set; }

        [BindProperty, Required]
        public string PhoneNumber { get; set; }

        [BindProperty]
        public DateTime BirthDate { get; set; }

        [BindProperty]
        public string RoleType { get; set; }

        [BindProperty]
        public string? RollNumber { get; set; }

        [BindProperty]
        public string? IBPNumber { get; set; }

        [BindProperty]
        public string? MCLEComplianceNumber { get; set; }

        [BindProperty]
        public DateTime? MCLEDate { get; set; }

        [BindProperty]
        public string? PTRNumber { get; set; }

        [BindProperty]
        public DateTime? PTRDate { get; set; }

        [BindProperty]
        public string? PTRLocation { get; set; }

        [BindProperty]
        public string? CommissionNumber { get; set; }

        [BindProperty]
        public DateTime? CommissionExpiration { get; set; }

        [BindProperty]
        public string? OfficeAddress { get; set; }

        [BindProperty]
        public bool IsEighteenYearsOrOlder { get; set; }
    }
}