import { useNavigate } from "react-router";
import { useSupervisorQuestions } from "../contexts/SupervisorQuestionsContext";
import { useUserSettings } from "../hooks/useUserSettings";
import type { PendingQuestion } from "../lib/api";

export function SupervisorQuestionNotification() {
  const navigate = useNavigate();
  const { notificationQuestions, dismissNotification } = useSupervisorQuestions();
  const { settings } = useUserSettings();
  const documentMode = settings?.documentModeEnabled ?? false;

  // Filter out contract_complete questions - they are displayed on the task page instead
  const filteredQuestions = notificationQuestions.filter(
    (q) => q.questionType !== "contract_complete"
  );

  if (filteredQuestions.length === 0) {
    return null;
  }

  const handleGoToTask = (q: PendingQuestion) => {
    dismissNotification(q.questionId);
    // In document mode, route directly to the directive folder page with
    // the task selected — that's the canonical surface where the question
    // is answered (same comment/interrupt UI as the task stream). Fall
    // back to /exec for non-doc-mode users or for tasks not tied to a
    // directive.
    if (documentMode && q.directiveId) {
      navigate(`/directives/${q.directiveId}?task=${q.taskId}`);
    } else {
      navigate(`/exec/${q.taskId}`);
    }
  };

  return (
    <div className="fixed bottom-4 right-4 z-50 max-w-md space-y-2">
      {filteredQuestions.map((question) => (
        <div
          key={question.questionId}
          className="bg-[#0d1b2d] border border-amber-500/50 rounded-lg shadow-lg overflow-hidden"
        >
          {/* Header */}
          <div className="flex items-center justify-between px-4 py-3 bg-amber-900/30">
            <div className="flex items-center gap-2">
              <span className="text-amber-400 text-lg">?</span>
              <span className="font-mono text-sm text-amber-300 uppercase">
                Task needs input
              </span>
            </div>
            <button
              onClick={() => handleGoToTask(question)}
              className="px-3 py-1 font-mono text-xs text-amber-400 border border-amber-500/30 hover:border-amber-400/50 hover:bg-amber-900/20 transition-colors uppercase"
            >
              View Task
            </button>
          </div>

          {/* Question preview */}
          <div className="px-4 py-3">
            {question.context && (
              <div className="text-xs text-[#8b949e] font-mono mb-1 uppercase">
                {question.context}
              </div>
            )}
            <p className="text-sm text-[#dbe7ff] font-mono line-clamp-2">
              {question.question}
            </p>
          </div>
        </div>
      ))}
    </div>
  );
}
