import {
  AdminBookingServiceError,
  listAdminBookings,
} from '@/modules/admin-booking/services/admin-booking.service';
import type { AppContext } from '@/types/hono';
import type { ListAdminBookingsQuery } from '@repo/api-contract';

type ComplexIdParams = { complexId: string };

export async function listAdminBookingsHandler(c: AppContext) {
  const { complexId } = c.req.valid('param' as never) as ComplexIdParams;
  const query = c.req.valid('query' as never) as ListAdminBookingsQuery;
  const user = c.get('user');

  try {
    const response = await listAdminBookings(user.id, complexId, query);
    return c.json(response);
  } catch (error) {
    if (error instanceof AdminBookingServiceError) {
      return c.json({ message: error.message }, error.status);
    }
    throw error;
  }
}
