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

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

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

  try {
    const result = await resendComplexInvitation(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;
  }
}
