'use client'

import { LessonSession, LessonStatus } from '@/lib/companion/types'
import { LessonStartForm } from './LessonStartForm'
import { LessonActiveView } from './LessonActiveView'
import { LessonEndedView } from './LessonEndedView'

interface LessonContainerProps {
  session: LessonSession | null
  onStartLesson: (data: { classId: string; subject: string; topic?: string; templateId?: string }) => void
  onEndLesson: () => void
  onPauseToggle: () => void
  onExtendTime: (minutes: number) => void
  onSkipPhase: () => void
  onSaveReflection: (rating: number, notes: string, nextSteps: string) => void
  onAddHomework: (title: string, dueDate: string) => void
  onRemoveHomework: (id: string) => void
  loading?: boolean
}

export function LessonContainer({
  session,
  onStartLesson,
  onEndLesson,
  onPauseToggle,
  onExtendTime,
  onSkipPhase,
  onSaveReflection,
  onAddHomework,
  onRemoveHomework,
  loading,
}: LessonContainerProps) {
  // Determine which view to show based on session state
  const getView = (): 'start' | 'active' | 'ended' => {
    if (!session) return 'start'

    const status = session.status
    if (status === 'completed') return 'ended'
    if (status === 'not_started') return 'start'

    return 'active'
  }

  const view = getView()

  if (view === 'start') {
    return (
      <LessonStartForm
        onStart={onStartLesson}
        loading={loading}
      />
    )
  }

  if (view === 'ended' && session) {
    return (
      <LessonEndedView
        session={session}
        onSaveReflection={onSaveReflection}
        onAddHomework={onAddHomework}
        onRemoveHomework={onRemoveHomework}
        onStartNew={() => onEndLesson()} // This will clear the session and show start form
      />
    )
  }

  if (session) {
    return (
      <LessonActiveView
        session={session}
        onPauseToggle={onPauseToggle}
        onExtendTime={onExtendTime}
        onSkipPhase={onSkipPhase}
        onEndLesson={onEndLesson}
      />
    )
  }

  return null
}
