using Knot.Contracts.Auth.Application.Auth.DTOs;
using Knot.Contracts.Auth.Application.Auth.DTOs;
using Knot.Shared.Kernel;

namespace Knot.Modules.Auth.Application.Users.GetMe;

public sealed record GetMeQuery(Guid UserId) : IQuery<AuthResponseDto>;

internal sealed class GetMeQueryHandler : IQueryHandler<GetMeQuery, AuthResponseDto>
{
    private readonly IUserRepository _userRepository;

    public GetMeQueryHandler(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public async Task<Result<AuthResponseDto>> Handle(GetMeQuery request, CancellationToken cancellationToken)
    {
        var user = await _userRepository.GetByIdAsync(request.UserId, cancellationToken);
        if (user == null)
        {
            return Result.Failure<AuthResponseDto>(AuthErrors.UserNotFound);
        }

        var response = new AuthResponseDto
        {
            AccessToken = string.Empty,
            RefreshToken = string.Empty,
            UserId = user.Id,
            Username = user.Username,
            DisplayName = user.DisplayName,
            Avatar = user.Avatar
        };

        return Result.Success(response);
    }
}
