import { Booking, Prisma, PublicUser, Station } from "@repo/db";
import axios from "axios";

export const getBookingsAPI = async (filter: Prisma.BookingWhereInput) => {
	const res = await axios.get<
		(Booking & {
			Station: Station;
			User: PublicUser;
		})[]
	>("/api/bookings", {
		params: {
			filter: JSON.stringify(filter),
		},
	});
	if (res.status !== 200) {
		throw new Error("Failed to fetch stations");
	}
	return res.data;
};

export const createBookingAPI = async (booking: Omit<Prisma.BookingCreateInput, "User">) => {
	const response = await axios.post("/api/bookings", booking);
	console.log("Response from createBookingAPI:", response);
	if (response.status !== 201) {
		console.error("Error creating booking:", response);
		throw new Error("Failed to create booking");
	}
	console.log("Booking created:", response.data);
	return response.data;
};

export const deleteBookingAPI = async (bookingId: string) => {
	const response = await axios.delete(`/api/bookings/${bookingId}`);
	if (!response.status.toString().startsWith("2")) {
		throw new Error("Failed to delete booking");
	}
	return bookingId;
};
