using EnotaryoPH.Data;
using EnotaryoPH.Web.Common.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace EnotaryoPH.Web.Pages.Principal.NotaryoSteps
{
    public class UploadIdentificationModel : PageModel
    {
        private readonly ICurrentUserService _currentUserService;
        private readonly NotaryoDBContext _notaryoDBContext;

        public UploadIdentificationModel(NotaryoDBContext notaryoDBContext, ICurrentUserService currentUserService)
        {
            _notaryoDBContext = notaryoDBContext;
            _currentUserService = currentUserService;
        }

        public async Task<IActionResult> OnGetAsync()
        {
            var user = _notaryoDBContext.Users.AsNoTracking().FirstOrDefault(u => u.User_UID == _currentUserService.GetUser_UID());
            if (user == null)
            {
                return BadRequest();
            }
            ExistingIdentificationDocuments = GetIdentityDocumentsByUserID(user.UserID);

            NewIdentificationDocument = new IdentificationDocumentModel
            {
                IdentificationTypes = GetIdentificationDocumentTypes()
            };

            UploadNewIdentification = true;

            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (UploadNewIdentification)
            {
                if (!ModelState.IsValid)
                {
                    NewIdentificationDocument = new IdentificationDocumentModel
                    {
                        IdentificationTypes = GetIdentificationDocumentTypes()
                    };
                    return Page();
                }
            }

            return RedirectToPage("/Principal/NotaryoSteps/TakeSelfie");
        }

        private List<SelectListItem> GetIdentificationDocumentTypes()
        {
            var lookupIdentificationTypes = _notaryoDBContext.LookupData.AsNoTracking().Include(e => e.LookupDataValues).FirstOrDefault(e => e.Name == "Identification Types");
            return lookupIdentificationTypes.LookupDataValues
                .ConvertAll(m => new SelectListItem
                {
                    Text = m.Title.DefaultIfEmpty(m.Value),
                    Value = m.Value
                });
        }

        private List<IdentificationDocumentModel> GetIdentityDocumentsByUserID(int userID) =>
                                _notaryoDBContext.IdentificationDocuments.AsNoTracking()
                    .Where(d => d.UserID == userID)
                    .Select(d => new IdentificationDocumentModel(d)).ToList();

        public int ExistingIdentificationDocumentCount => ExistingIdentificationDocuments.Count;

        public List<IdentificationDocumentModel> ExistingIdentificationDocuments { get; set; } = [];

        [BindProperty(SupportsGet = true)]
        public Guid IdentificationDocument_UID { get; set; }

        [BindProperty]
        public IdentificationDocumentModel NewIdentificationDocument { get; set; }

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

        public Guid Transaction_UID { get; private set; }
    }
}