import InjectMessage from '@/actionlog/InjectMessage'
import { NonIdealState } from '@blueprintjs/core'
import { css } from '@emotion/css'
import { useTypedQuery } from '@inject/graphql/graphql'
import { GetSingleActionLog } from '@inject/graphql/queries'
import Container from '@inject/shared/components/Container'
import { useMemo, type FC } from 'react'

const wrapper = css`
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  height: 100%;
  width: 100%;
`

interface InjectMessageViewProps {
  teamId: string
  exerciseId: string
  actionLogId: string
  inInstructor?: boolean
}

const InjectMessageView: FC<InjectMessageViewProps> = ({
  teamId,
  exerciseId,
  actionLogId,
  inInstructor,
}) => {
  const [{ data }] = useTypedQuery({
    query: GetSingleActionLog,
    variables: {
      logId: actionLogId,
    },
    context: useMemo(
      () => ({
        suspense: true,
      }),
      []
    ),
  })

  if (data?.actionLog.team.id !== teamId) {
    return (
      <NonIdealState
        title='ActionLog for different team'
        description='Hmm, this ActionLog seems to be belong to another team'
      />
    )
  }

  return (
    <Container>
      <div className={wrapper}>
        {data?.actionLog && (
          <InjectMessage
            exerciseId={exerciseId}
            teamId={teamId}
            fill
            actionLog={data.actionLog}
            inInstructor={!!inInstructor}
          />
        )}
      </div>
    </Container>
  )
}

export default InjectMessageView
