namespace Netina.Repository.Handlers.Addresses;

public class CreateAddressCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
    : IRequestHandler<CreateAddressCommand, bool>
{
    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;
    }
}