package handler

import (
	"context"
	"solidarity-service/internal"

	"github.com/paulmach/orb/geojson"
)

func (s *SolidarityServiceHandler) GetPassenger(ctx context.Context, id string) (passenger internal.Passenger, err error) {
	passenger, err = s.Storage.GetPassenger(id)
	if err != nil {
		return internal.Passenger{}, err
	}
	return passenger, nil
}

func (s *SolidarityServiceHandler) GetAllPassengers(ctx context.Context) (passengers []internal.Passenger, err error) {
	passengers, err = s.Storage.GetAllPassengers()
	if err != nil {
		return []internal.Passenger{}, err
	}
	return passengers, nil
}

func (s *SolidarityServiceHandler) GetDriver(ctx context.Context, id string) (driver internal.Driver, err error) {
	driver, err = s.Storage.GetDriver(id)
	if err != nil {
		return internal.Driver{}, err
	}
	return driver, nil
}

func (s *SolidarityServiceHandler) SetDriverAvailabilities(ctx context.Context, driver internal.Driver) (err error) {
	driver.Driver.Operator = "internal-grpc"
	err = s.Storage.CreateDriver(driver)
	return err
}

func (s *SolidarityServiceHandler) SetPassengerTrip(ctx context.Context, passenger internal.Passenger) (err error) {
	passenger.Passenger.Operator = "internal-grpc"
	err = s.Storage.CreatePassenger(passenger)
	if err != nil {
		return err
	}
	return nil
}

func (s *SolidarityServiceHandler) CreateBooking(ctx context.Context, bookingRequest internal.BookingRequest) (passenger internal.Passenger, driver internal.Driver, err error) {
	bookingRequest.Operator = "internal-grpc"
	err = s.Storage.CreateBooking(bookingRequest)
	if err != nil {
		return internal.Passenger{}, internal.Driver{}, err
	}
	passenger, err = s.Storage.GetPassenger(bookingRequest.Passenger_id)
	if err != nil {
		return internal.Passenger{}, internal.Driver{}, err
	}
	driver, err = s.Storage.GetDriver(bookingRequest.Driver_id)
	if err != nil {
		return internal.Passenger{}, internal.Driver{}, err
	}
	return passenger, driver, nil
}

func (s *SolidarityServiceHandler) UpdateBooking(ctx context.Context, bookingId string, status internal.BookingStatus) (err error) {
	err = s.Storage.UpdateBookingStatus(bookingId, status)
	if err != nil {
		return err
	}
	return nil
}

func (s *SolidarityServiceHandler) GetBooking(ctx context.Context, bookingId string) (booking internal.Booking, err error) {
	booking, err = s.Storage.GetBooking(bookingId)
	if err != nil {
		return internal.Booking{}, err
	}
	return booking, nil
}

func (s *SolidarityServiceHandler) GetBookingsByStatus(ctx context.Context, status string, filterType string, id string) (bookings []internal.Booking, err error) {
	bookings, err = s.Storage.FilterUserBookingsByStatus(filterType, internal.BookingStatus(status), id)
	if err != nil {
		return nil, err
	}
	return bookings, nil
}

func (s *SolidarityServiceHandler) GetDriverJourneys(ctx context.Context, departure_route *geojson.Feature, departure_date int64) (drivers []internal.Driver, err error) {
	drivers, err = s.Storage.DriverJourneys(departure_route, departure_date)
	if err != nil {
		return nil, err
	}
	return drivers, nil
}
