'use client';

import { useVisualization } from '@/lib/visualizations/use-visualization';
import type { AlgorithmDefinition } from '@/lib/visualizations/types';
import { VisualizationContainer } from '../core/visualization-container';
import { DecisionTreeView } from '../data-structures/decision-tree-view';
import { ArrayView } from '../data-structures/array-view';

interface BacktrackingVisualizationProps {
  algorithm: AlgorithmDefinition;
  className?: string;
}

export function BacktrackingVisualization({
  algorithm,
  className,
}: BacktrackingVisualizationProps) {
  const {
    currentStep,
    currentStepIndex,
    totalSteps,
    playback,
    controls,
    currentPhase,
    progress,
  } = useVisualization(algorithm);

  const { dataState } = currentStep;
  const decisionTree = dataState.decisionTrees?.[0] ?? null;
  const inputArray = dataState.arrays?.[0] ?? null;

  return (
    <VisualizationContainer
      title={algorithm.title}
      pattern={algorithm.pattern}
      code={algorithm.code}
      currentLine={currentStep.codeLine}
      highlightLines={currentStep.codeHighlightLines}
      explanation={currentStep.explanation}
      decision={currentStep.decision}
      phase={currentPhase}
      variables={dataState.variables}
      currentStepIndex={currentStepIndex}
      totalSteps={totalSteps}
      isPlaying={playback.isPlaying}
      speed={playback.speed}
      controls={controls}
      progress={progress}
      className={className}
    >
      <div className="flex flex-col items-center gap-6">
        {/* Input array (if present) */}
        {inputArray && (
          <ArrayView
            array={inputArray}
            pointers={dataState.pointers}
            elementSize="sm"
          />
        )}

        {/* Decision tree */}
        {decisionTree && (
          <DecisionTreeView tree={decisionTree} />
        )}
      </div>
    </VisualizationContainer>
  );
}
