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

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

  try {
    const result = await resendOnboardingOtp(payload);
    return c.json(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;
  }
}
