import { useNavigate } from '@tanstack/react-router'
import { Card } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider } from '@/components/ui/tooltip'
import { formatRelativeTime } from '@/lib/utils'
import type { WaitingForInputItem } from './types'

interface Props {
  items: WaitingForInputItem[]
}

const TRUNCATE_LENGTH = 120

export function HQWaitingForInputSection({ items }: Props) {
  const navigate = useNavigate()

  return (
    <div className="space-y-3">
      <h2 className="text-sm font-medium text-muted-foreground uppercase tracking-wide">
        Waiting for Input
      </h2>
      <div className="space-y-2">
        {items.map((item) => {
          const isTruncated = item.questionText.length > TRUNCATE_LENGTH
          const displayText = isTruncated
            ? item.questionText.slice(0, TRUNCATE_LENGTH) + '…'
            : item.questionText

          return (
            <Card key={item.agentId} className="p-4 flex items-center justify-between gap-4">
              <div className="min-w-0 flex-1">
                <p className="font-semibold text-sm">
                  {item.agentName}
                  {item.initiativeId && item.initiativeName && (
                    <span className="font-normal text-muted-foreground"> · {item.initiativeName}</span>
                  )}
                </p>
                <TooltipProvider>
                  <Tooltip>
                    <TooltipTrigger asChild>
                      <p className="text-sm text-muted-foreground mt-1 cursor-default">
                        {displayText}
                      </p>
                    </TooltipTrigger>
                    {isTruncated && (
                      <TooltipContent forceMount>
                        {item.questionText}
                      </TooltipContent>
                    )}
                  </Tooltip>
                </TooltipProvider>
                <p className="text-xs text-muted-foreground mt-1">
                  waiting {formatRelativeTime(item.waitingSince)}
                </p>
              </div>
              <Button
                variant="default"
                size="sm"
                onClick={() => navigate({ to: '/inbox' })}
              >
                Answer
              </Button>
            </Card>
          )
        })}
      </div>
    </div>
  )
}
