import type {
  AdminBooking,
  CancelPublicBookingInput,
  CreateAdminBookingInput,
  CreatePublicBookingInput,
  ListRecurringGroupsResponse,
  PublicAvailabilityResponse,
  PublicBooking,
  PublicBookingConfirmation,
  RecurringBookingGroup,
  UpdateAdminBookingStatusInput,
  UpdateRecurringGroupInput,
} from '@repo/api-contract';
import type { CreateRecurringBookingInput } from '@repo/api-contract';
import { http } from '../http';

export async function getAvailability(
  complexSlug: string,
  params: { date: string; sportId?: string }
) {
  const response = await http.get<PublicAvailabilityResponse>(
    `/api/public-bookings/complex/${complexSlug}/availability`,
    { params }
  );
  return response.data;
}

export async function createPublic(complexSlug: string, payload: CreatePublicBookingInput) {
  const response = await http.post<PublicBooking>(
    `/api/public-bookings/complex/${complexSlug}`,
    payload
  );
  return response.data;
}

export async function getConfirmation(complexSlug: string, bookingCode: string) {
  const response = await http.get<PublicBookingConfirmation>(
    `/api/public-bookings/complex/${complexSlug}/confirmation/${bookingCode}`
  );
  return response.data;
}

export async function listByComplex(complexId: string, params: { fromDate: string }) {
  const response = await http.get<{ bookings: AdminBooking[] }>(
    `/api/admin-bookings/complex/${complexId}`,
    { params }
  );
  return response.data;
}

export async function createAdmin(complexId: string, payload: CreateAdminBookingInput) {
  const response = await http.post<AdminBooking>(
    `/api/admin-bookings/complex/${complexId}`,
    payload
  );
  return response.data;
}

export async function createRecurring(complexId: string, payload: CreateRecurringBookingInput) {
  const response = await http.post<RecurringBookingGroup>(
    `/api/admin-bookings/complex/${complexId}/recurring`,
    payload
  );
  return response.data;
}

export async function cancelRecurringGroup(groupId: string) {
  const response = await http.post<{ ok: boolean }>(
    `/api/admin-bookings/recurring/${groupId}/cancel`
  );
  return response.data;
}

export async function listRecurringGroups(complexId: string) {
  const response = await http.get<ListRecurringGroupsResponse>(
    `/api/admin-bookings/complex/${complexId}/recurring`
  );
  return response.data;
}

export async function updateRecurringGroup(groupId: string, payload: UpdateRecurringGroupInput) {
  const response = await http.patch<RecurringBookingGroup>(
    `/api/admin-bookings/recurring/${groupId}`,
    payload
  );
  return response.data;
}

export async function cancelPublic(complexSlug: string, payload: CancelPublicBookingInput) {
  const response = await http.post<PublicBooking>(
    `/api/public-bookings/complex/${complexSlug}/cancel`,
    payload
  );
  return response.data;
}

export async function updateStatus(bookingId: string, payload: UpdateAdminBookingStatusInput) {
  const response = await http.patch<AdminBooking>(
    `/api/admin-bookings/${bookingId}/status`,
    payload
  );
  return response.data;
}
