namespace NetinaShop.Repository.Handlers.Addresses;

public class CreateAddressCommandHandler : IRequestHandler<CreateAddressCommand,bool>
{
    private readonly IRepositoryWrapper _repositoryWrapper;
    private readonly ICurrentUserService _currentUserService;

    public CreateAddressCommandHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService)
    {
        _repositoryWrapper = repositoryWrapper;
        _currentUserService = currentUserService;
    }
    public async Task<bool> Handle(CreateAddressCommand request, CancellationToken cancellationToken)
    {
        if (_currentUserService.UserId == null)
            throw new AppException("User id notfound", ApiResultStatusCode.BadRequest);
        if (!Guid.TryParse(_currentUserService.UserId, out Guid userId))
            throw new AppException("User id wrong", ApiResultStatusCode.BadRequest);


        var ent = UserAddress.Create(request.Address, request.PostalCode, request.ReceiverFullName,
            request.ReceiverPhoneNumber, request.LocationLat, request.LocationLong, request.Province, request.City,
            request.Plaque, request.BuildingUnit, userId);

        _repositoryWrapper.SetRepository<UserAddress>().Add(ent);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        return false;
    }
}