using Indotalent.Data.Entities;
using MediatR;
using Microsoft.AspNetCore.Identity;

namespace Indotalent.Features.Setting.SystemUser.Cqrs;

public class UpdateSystemUserRequest
{
    public string? Id { get; set; }
    public string? FullName { get; set; }
    public string? Email { get; set; }
    public string? PhoneNumber { get; set; }
    public string? UserName { get; set; }
    public bool EmailConfirmed { get; set; }
    public bool PhoneNumberConfirmed { get; set; }
    public bool TwoFactorEnabled { get; set; }
    public bool IsActive { get; set; }
    public bool LockoutEnabled { get; set; }
    public DateTimeOffset? LockoutEnd { get; set; }
    public int AccessFailedCount { get; set; }
    public string? SsoIdFirebase { get; set; }
    public string? SsoIdKeycloak { get; set; }
    public string? SsoIdAzure { get; set; }
    public string? SsoIdAws { get; set; }
    public string? SsoIdOther1 { get; set; }
    public string? SsoIdOther2 { get; set; }
    public string? SsoIdOther3 { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime? LastLoginAt { get; set; }
    public DateTime? UpdatedAt { get; set; }
    public List<string> UserRoles { get; set; } = new();
    public string? AvatarFile { get; set; }
}

public class UpdateSystemUserResponse
{
    public string? Id { get; set; }
    public bool Success { get; set; }
}


public record UpdateSystemUserCommand(UpdateSystemUserRequest Data) : IRequest<UpdateSystemUserResponse>;

public class UpdateSystemUserHandler : IRequestHandler<UpdateSystemUserCommand, UpdateSystemUserResponse>
{
    private readonly UserManager<ApplicationUser> _userManager;
    public UpdateSystemUserHandler(UserManager<ApplicationUser> userManager) => _userManager = userManager;

    public async Task<UpdateSystemUserResponse> Handle(UpdateSystemUserCommand request, CancellationToken cancellationToken)
    {
        var user = await _userManager.FindByIdAsync(request.Data.Id ?? string.Empty);

        if (user == null)
        {
            return new UpdateSystemUserResponse { Id = request.Data.Id, Success = false };
        }

        user.FullName = request.Data.FullName;
        user.PhoneNumber = request.Data.PhoneNumber;
        user.IsActive = request.Data.IsActive;
        user.EmailConfirmed = request.Data.EmailConfirmed;
        user.PhoneNumberConfirmed = request.Data.PhoneNumberConfirmed;
        user.TwoFactorEnabled = request.Data.TwoFactorEnabled;
        user.LockoutEnabled = request.Data.LockoutEnabled;
        user.LockoutEnd = request.Data.LockoutEnd;

        user.SsoIdFirebase = request.Data.SsoIdFirebase;
        user.SsoIdKeycloak = request.Data.SsoIdKeycloak;
        user.SsoIdAzure = request.Data.SsoIdAzure;
        user.SsoIdAws = request.Data.SsoIdAws;
        user.SsoIdOther1 = request.Data.SsoIdOther1;
        user.SsoIdOther2 = request.Data.SsoIdOther2;
        user.SsoIdOther3 = request.Data.SsoIdOther3;

        user.UpdatedAt = DateTime.Now;

        user.AvatarFile = request.Data.AvatarFile;

        var result = await _userManager.UpdateAsync(user);

        return new UpdateSystemUserResponse
        {
            Id = user.Id,
            Success = result.Succeeded
        };
    }
}