import { serverFetch } from '@/lib/api/server';
import { unwrap, type ApiEnvelope } from '@/lib/api/types';
import type { BookingDetailDto } from '../types';

const BOOKINGS = '/api/v1/bookings';

/**
 * Server-side reads for the bookings domain — used to **prefetch the booking detail in an RSC** and hand
 * it to the client tree via `initialData`, removing a client round-trip on first paint (f0 pattern). Only
 * the detail is prefetched; the EVV mutations and the gated care-instructions read stay on the client.
 *
 * Not wired into the pages while the domain is mock-primary (an RSC cannot read the in-memory mock store);
 * it becomes the first-paint source the moment `USE_BOOKINGS_MOCK` flips to `false`. Kept separate from
 * `clientApi.ts` — Next.js enforces the `serverFetch`/`clientFetch` environment boundary at build time.
 */
export const bookingsServerApi = {
  getBookingDetail: async (id: number): Promise<BookingDetailDto> =>
    unwrap(await serverFetch<ApiEnvelope<BookingDetailDto>>(`${BOOKINGS}/get/${id}`)),
};
