import { CourtServiceError, listCourtsByComplex } from '@/modules/court/services/court.service';
import type { AppContext } from '@/types/hono';

type ComplexIdParams = { complexId: string };

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

  try {
    const courts = await listCourtsByComplex(complexId, user.id);
    return c.json(courts);
  } catch (error) {
    if (error instanceof CourtServiceError) {
      return c.json({ message: error.message }, error.status);
    }
    throw error;
  }
}
