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

type ComplexIdParams = { complexId: string };

export async function createAdminBookingHandler(c: AppContext) {
  const { complexId } = c.req.valid('param' as never) as ComplexIdParams;
  const payload = c.req.valid('json' as never) as CreateAdminBookingInput;
  const user = c.get('user');

  try {
    const booking = await createAdminBooking(user.id, complexId, payload);
    return c.json(booking, 201);
  } catch (error) {
    if (error instanceof AdminBookingServiceError) {
      return c.json({ message: error.message }, error.status);
    }
    throw error;
  }
}
