import {
  PublicBookingServiceError,
  listPublicAvailability,
} from '@/modules/public-booking/services/public-booking.service';
import type { AppContext } from '@/types/hono';
import type { PublicAvailabilityQuery } from '@repo/api-contract';

type ComplexSlugParams = { complexSlug: string };

export async function listPublicAvailabilityHandler(c: AppContext) {
  const { complexSlug } = c.req.valid('param' as never) as ComplexSlugParams;
  const query = c.req.valid('query' as never) as PublicAvailabilityQuery;

  try {
    const availability = await listPublicAvailability(complexSlug, query);
    return c.json(availability);
  } catch (error) {
    if (error instanceof PublicBookingServiceError) {
      return c.json({ message: error.message }, error.status);
    }

    throw error;
  }
}
