import {
  OnboardingError,
  completeOnboarding,
} from '@/modules/onboarding/services/onboarding.service';
import type { AppContext } from '@/types/hono';
import type { OnboardingCompleteInput } from '@repo/api-contract';

export async function completeOnboardingHandler(c: AppContext) {
  const payload = c.req.valid('json' as never) as OnboardingCompleteInput;

  try {
    const result = await completeOnboarding(payload);
    return c.json({
      message: 'Onboarding completado correctamente.',
      ...result,
    });
  } catch (error) {
    if (error instanceof OnboardingError) {
      return c.json({ message: error.message }, error.status);
    }

    if (error instanceof Error) {
      return c.json({ message: error.message }, 400);
    }
    throw error;
  }
}
