import {
  ComplexMembersError,
  cancelComplexInvitation,
} from '@/modules/complex/services/complex-members.service';
import type { AppContext } from '@/types/hono';
import { z } from 'zod';

const cancelInvitationParamsSchema = z.object({
  id: z.uuid(),
  invitationId: z.uuid(),
});

export async function cancelComplexInvitationHandler(c: AppContext) {
  const user = c.get('user');
  const params = cancelInvitationParamsSchema.parse(c.req.param());

  try {
    const result = await cancelComplexInvitation(user.id, params.id, params.invitationId);
    return c.json(result);
  } catch (error) {
    if (error instanceof ComplexMembersError) {
      return c.json({ message: error.message }, { status: error.status });
    }

    throw error;
  }
}
