using MediatR;
using Modules.User.Application.Gateways;

namespace Modules.User.Application.Commands.Session;

public class DeleteSessionCommand : IRequest<bool>
{
    public Guid AccountId { get; init; }
    public Guid SessionId { get; init; }
}

public class DeleteSessionCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork) : IRequestHandler<DeleteSessionCommand, bool>
{
    public async Task<bool> Handle(DeleteSessionCommand request, CancellationToken cancellationToken)
    {
        var user = await userRepository.GetByAccountIdAsync(request.AccountId, cancellationToken);
        if (user == null) return false;
        //TODO: if (user == null) throw new UserNotFoundException(); //"User not found"
        var success = user.DeleteSession(request.SessionId);
        if (!success) return false;
        //TODO: if (!success) throw new Exception("Session not found");
        
        await userRepository.SaveAsync(user, cancellationToken);
        await unitOfWork.SaveChangesAsync(cancellationToken);
        return true;
    }
}