extends Node
class_name ReservationService

@onready var _main: Main = get_tree().get_current_scene()

func is_time_busy(new_start_time_minutes, new_finish_time_minutes, room_id=null, date=null):
	var selected_room = _main.get_selected_room()
	var selected_date = _main.get_selected_date()

	var reservation_repo = _main.get_reservation_repo()
	var reservations = await reservation_repo.list_reservations({
		"date": date if date != null else selected_date,
		"room_id": room_id if room_id != null else selected_room.id
	})
	var selected_reservation_id = reservation_repo.get_selected_reservation_id()

	for reservation in reservations:
		if reservation.id == selected_reservation_id:
			continue

		var other_start_time = reservation.start_time
		var other_finish_time = reservation.finish_time
		var other_start_time_minutes = other_start_time.hours*60 + other_start_time.minutes
		var other_finish_time_minutes = other_finish_time.hours*60 + other_finish_time.minutes

		var is_busy = (
			(
				new_start_time_minutes >= other_start_time_minutes and
				new_start_time_minutes < other_finish_time_minutes
			) or (
				new_finish_time_minutes > other_start_time_minutes and 
				new_finish_time_minutes <= other_finish_time_minutes
			) or (
				new_start_time_minutes >= other_start_time_minutes and 
				new_finish_time_minutes <= other_finish_time_minutes
			) or (
				new_start_time_minutes <= other_start_time_minutes and 
				new_finish_time_minutes >= other_finish_time_minutes
			)
 		)

		if is_busy:
			return true

	return false
