import InstructorQuestionnaire from '@/instructor/InstructorQuestionnaire'
import type { ActionLog } from '@inject/graphql/fragment-types'
import { type FC } from 'react'
import EmailContent from './EmailContent'
import InjectContent from './InjectContent'
import ToolContent from './ToolContent'
import TraineeQuestionnaireContent from './TraineeQuestionnaireContent'

interface ContentProps {
  actionLog: ActionLog
  exerciseId: string
  teamId: string
  inInstructor: boolean
  onClose?: () => void
}

const Content: FC<ContentProps> = ({
  actionLog,
  exerciseId,
  teamId,
  inInstructor,
  onClose,
}) => {
  switch (actionLog.details.__typename) {
    case 'InjectDetailsType':
      return (
        <InjectContent
          logId={actionLog.id}
          {...actionLog.details}
          teamId={teamId}
          exerciseId={exerciseId}
          inInstructor={inInstructor}
          onClose={onClose}
        />
      )
    case 'CustomInjectDetailsType':
      return (
        <InjectContent
          logId={actionLog.id}
          {...actionLog.details}
          teamId={teamId}
          exerciseId={exerciseId}
          inInstructor={inInstructor}
          onClose={onClose}
        />
      )
    case 'ToolDetailsType':
      return (
        // can't be in overlay, so no onClose
        <ToolContent
          details={actionLog.details}
          teamId={teamId}
          exerciseId={exerciseId}
          inInstructor={inInstructor}
        />
      )
    case 'TeamQuestionnaireStateType': {
      const teamState = actionLog.details
      return inInstructor ? (
        // can't be in overlay, so no onClose
        <InstructorQuestionnaire
          teamStateId={teamState.id}
          exerciseId={exerciseId}
          questionnaireId={teamState.questionnaire.id}
          teamId={teamId}
          status={teamState.status}
          content={teamState.questionnaire.content}
          questions={teamState.questionnaire.questions}
          teamAnswers={teamState.answers}
          relatedMilestones={teamState.relatedMilestones}
        />
      ) : (
        <TraineeQuestionnaireContent
          teamState={actionLog.details}
          teamId={teamId}
          exerciseId={exerciseId}
          onClose={onClose}
        />
      )
    }
    case 'EmailType':
      return (
        <EmailContent
          details={actionLog.details}
          teamId={teamId}
          exerciseId={exerciseId}
          inInstructor={inInstructor}
          onClose={onClose}
        />
      )
    default:
      throw new Error(`unknown actionLog type: ${actionLog.type}`)
  }
}

export default Content
