import {
  ComplexMembersError,
  acceptComplexInvitations,
} from '@/modules/complex/services/complex-members.service';
import type { AppContext } from '@/types/hono';
import type { AcceptComplexInvitationsInput } from '@repo/api-contract';

export async function acceptComplexInvitationsHandler(c: AppContext) {
  const user = c.get('user');
  const payload = c.req.valid('json' as never) as AcceptComplexInvitationsInput;

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

    throw error;
  }
}
