'use server';

import { Result } from '@/lib/contracts/Result';
import { CompleteOnboardingMutation } from '@/lib/mutations/onboarding/CompleteOnboardingMutation';
import { CompleteOnboardingInputDTO } from '@/lib/types/generated/CompleteOnboardingInputDTO';
import { revalidatePath } from 'next/cache';
import { routes } from '@/lib/routing/RouteConfig';

/**
 * Complete onboarding - thin wrapper around mutation
 * 
 * Pattern: Server Action → Mutation → Service → API Client
 * 
 * Authentication is handled automatically by the API via cookies.
 * The BaseApiClient includes credentials: 'include', so cookies are sent automatically.
 * If authentication fails, the API returns 401/403 which gets converted to domain errors.
 */
export async function completeOnboardingAction(
  input: CompleteOnboardingInputDTO
): Promise<Result<{ success: boolean }, string>> {
  const mutation = new CompleteOnboardingMutation();
  const result = await mutation.execute(input);
  
  if (result.isErr()) {
    return Result.err(result.getError());
  }
  
  revalidatePath(routes.protected.dashboard);
  return Result.ok({ success: true });
}