import { CourtServiceError, updateCourt } from '@/modules/court/services/court.service';
import type { AppContext } from '@/types/hono';
import type { UpdateCourtInput } from '@repo/api-contract';

type CourtIdParams = { id: string };

export async function updateCourtHandler(c: AppContext) {
  const { id } = c.req.valid('param' as never) as CourtIdParams;
  const payload = c.req.valid('json' as never) as UpdateCourtInput;
  const user = c.get('user');

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