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

const revokeParamsSchema = z.object({
  id: z.uuid(),
  userId: z.string().min(1),
});

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

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

    throw error;
  }
}
