'use client';

import type { InferenceResult } from '@/types/inference';
import { isClassificationTask } from '@/lib/classification';
import { CameraOverlay } from './camera-overlay';
import { ClassificationOverlay } from './classification-overlay';

interface InferenceOverlayProps {
  result: InferenceResult | null | undefined;
  width: number;
  height: number;
  confidenceThreshold: number;
}

/**
 * Dispatches the frame overlay based on the task type reported by the
 * inference result itself.
 *
 * `result.taskType` is preferred over the model metadata because it reflects
 * what the Python post-processor actually did — metadata can disagree with the
 * executed code path.
 *
 * Note the branch is written as "is this classification?" rather than "is this
 * detection?" on purpose: the detection task type string has been inconsistent
 * across the stack (`detection` vs `object_detection`, plan R-4), so detection
 * is the fall-through default and stays correct under either spelling.
 */
export function InferenceOverlay({
  result,
  width,
  height,
  confidenceThreshold,
}: InferenceOverlayProps) {
  if (isClassificationTask(result?.taskType)) {
    return (
      <ClassificationOverlay
        classifications={result?.classifications || []}
        width={width}
        height={height}
        confidenceThreshold={confidenceThreshold}
      />
    );
  }

  return (
    <CameraOverlay
      detections={result?.detections || []}
      width={width}
      height={height}
      confidenceThreshold={confidenceThreshold}
    />
  );
}
