package service

import (
	"fmt"

	"bitbucket.org/nemt/nemt-portal-api/domain/entity"
)

// userService is the domain service for user operations
type rideService struct {
	svc *Service
}

// newUserService returns an instance of userService
func newRideService(svc *Service) *rideService {
	return &rideService{
		svc: svc,
	}
}

// Save the ride for a expected user
func (s *rideService) Save(ride entity.Ride) (entity.Ride, error) {
	ride, err := s.svc.db.Rides().Save(ride)
	if err != nil {
		fmt.Println("Ride Service.Save: Error saving Ride: ", err.Error())
		return ride, err
	}

	user, err := s.svc.db.Users().GetByID(ride.CreatedUser.ID)
	if err != nil {
		fmt.Println("Ride Service.Save: Error getting User: ", err.Error())
		return ride, err
	}

	return s.GetByUUID(ride.UUID, user)
}

func (s *rideService) Update(hook entity.WebhookResponse) (entity.Ride, error) {
	user, err := s.svc.db.Users().GetByID(hook.Ride.CreatedUser.ID)
	if err != nil {
		return entity.Ride{}, err
	}

	ride, err := s.svc.db.Rides().Update(hook, user)
	if err != nil {
		return ride, err
	}

	return s.GetByUUID(ride.UUID, user)
}

// GetAll return all rides
func (s *rideService) GetAll(user entity.User) ([]entity.Ride, error) {
	return s.svc.db.Rides().GetAll(user)
}

func (s *rideService) GetByID(id int64, user entity.User) (entity.Ride, error) {
	return s.svc.db.Rides().GetByID(id, user)
}

// GetByUUID return a specific ride
func (s *rideService) GetByUUID(uuid string, user entity.User) (entity.Ride, error) {
	ride, err := s.svc.db.Rides().GetByUUID(uuid, user)
	if err != nil {
		fmt.Println("Ride Service.GetByUUID: Ride UUID: ", uuid)
		fmt.Println("Ride Service.GetByUUID: Error getting Ride: ", err.Error())
		return entity.Ride{}, err
	}

	return ride, nil
}

// GetByUUID return a specific ride
func (s *rideService) GetByUUIDAndUserUUID(UUID string, userUUID string) (entity.Ride, error) {
	return s.svc.db.Rides().GetByUUIDAndUserUUID(UUID, userUUID)
}

// GetByUUID return a specific ride
func (s *rideService) GetByInternalID(internalID string) (entity.Ride, error) {
	return s.svc.db.Rides().GetByInternalID(internalID)
}

// GetByUserID return a list of rides
func (s *rideService) GetByUserID(userID int64, user entity.User) ([]entity.Ride, error) {
	return s.svc.db.Rides().GetByUserID(userID, user)
}

func (s *rideService) GetByUserUUID(userUUID string, user entity.User) ([]entity.Ride, error) {
	return s.svc.db.Rides().GetByUserUUID(userUUID, user)
}

func (s *rideService) UpdateStatus(rideUUID string, status string) error {
	return s.svc.db.Rides().UpdateStatus(rideUUID, status)
}

func (s *rideService) GetLastRideByPhoneNumber(phoneNumber string) (entity.Ride, error) {
	return s.svc.db.Rides().GetLastRideByPhoneNumber(phoneNumber)
}

func (s *rideService) GetLastRideByDriversNumber(phoneNumber string) (entity.Ride, error) {
	return s.svc.db.Rides().GetLastRideByDriversNumber(phoneNumber)
}

func (s *rideService) GetByInternalPassengerID(internalPassengerID string) (entity.Ride, error) {
	return s.svc.db.Rides().GetByInternalPassengerID(internalPassengerID)
}

func (s *rideService) GetByVisitUUID(visitUUID string, user entity.User) ([]entity.Ride, error) {
	return s.svc.db.Rides().GetByVisitUUID(visitUUID, user)
}

func (s *rideService) GetByVisitUUIDAndTripType(visitUUID string, tripTypeKey string, user entity.User) (entity.Ride, error) {
	return s.svc.db.Rides().GetByVisitUUIDAndTripType(visitUUID, tripTypeKey, user)
}
