using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations.Schema;

namespace Infrastructure.Persistence.Identity
{
    public sealed class ApplicationUser : IdentityUser
    {
        [NotMapped]
        public string ID
        {
            get => Id;
            private set => Id = value;
        }

        public string? FullName { get; private set; }

        public bool IsDeleted { get; private set; } = false;

        // EF Core / Identity requires a public parameterless ctor
        public ApplicationUser() { }

        private ApplicationUser(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentException("ID is required.", nameof(id));
            }

            ID = id;
        }

        public static ApplicationUser Create(string id, string? fullName = null)
        {
            var user = new ApplicationUser(id);
            user.FullName = fullName;
            return user;
        }

        public void SetFullName(string? fullName)
        {
            FullName = string.IsNullOrWhiteSpace(fullName) ? null : fullName.Trim();
        }

        public void SetEmail(string? email)
        {
            Email = string.IsNullOrWhiteSpace(email) ? null : email.Trim();
            NormalizedEmail = Email?.ToUpperInvariant();
        }

        public void SetPhoneNumber(string? phoneNumber)
        {
            PhoneNumber = string.IsNullOrWhiteSpace(phoneNumber) ? null : phoneNumber.Trim();
        }

        public void SetDeleted(bool isDeleted)
        {
            IsDeleted = isDeleted;
        }

        public void SetEmailConfirmed(bool emailConfirmed)
        {
            EmailConfirmed = emailConfirmed;
        }

        public void SetLockoutEnd(bool lockoutEnd)
        {
            LockoutEnabled = lockoutEnd;
            LockoutEnd = lockoutEnd ? DateTimeOffset.MaxValue : null;
        }
    }
}