import { ActionIcon, Button, Drawer, Text, Textarea } from '@mantine/core';
import { IconArrowUp, IconMessages, IconPlus, IconShieldLock, IconTrash, IconX } from '@/components/icons';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useCallback, useEffect, useRef, useState, type FormEvent, type KeyboardEvent as ReactKeyboardEvent } from 'react';
import { Link, useLocation, useNavigate, useParams, useSearchParams } from 'react-router-dom';
import { MODULE_NAMES } from '../domain/agents';
import { errorMessage } from '../api/client';
import { askErrorWords, useAssistantAsk, useAssistantStatus, useRun } from '../api/hooks';
import { useShell } from '../components/shell/prefs';
import { TopBar } from '../components/shell/TopBar';
import { QueryState } from '../components/states';
import { runTitle, shortWhen } from '../domain/runName';
import { AnswerCard, ConfirmCard, ScreenLinks, Sources } from './ChatCards';
import {
createConversation, deleteConversation, getConversation, listConversations, sendMessage,
type ChatAction, type ChatContext, type ChatMessage, type ChatResult, type ConversationSummary,
} from './api';
import { CoworkerAvatar } from '../components/CoworkerAvatar';
import { COWORKERS } from '../domain/coworkers';
import { speakerFor, type Speaker } from './speaker';
import type { AskArrival } from './useAsk';
import './ask.css';
const STARTERS_PATCH = ['Is Procure-to-Pay ready for 26C?', 'What does this patch change in invoice matching?', 'What is not covered yet?', 'What is waiting for me?'];
const STARTERS_RUN = ['What is waiting for me in this test run?', 'Why did the requisition approval scenario fail?', 'What does this patch change in invoice matching?'];
/** A question that asks the co-worker to do something: it always goes to the built-in assistant, whose proposed actions
* keep their confirm cards. Everything else goes to Hermes when an admin switched Hermes on. */
const ACTION_WORDS = /^(please\s+)?(start|run|launch|re-?run|approve|skip|cancel|stop|add a note|note)\b/i;
/** Shown under the input only while Hermes is the one answering. */
export const HERMES_NOTICE = "Answered by the Hermes assistant using NVIDIA's AI model. Don't paste client data.";
/** What the co-worker looks at: the test run in the address (/assistant/:runId), or a business area (?module=). Context,
* never an instruction. */
function useAskContext() {
const { id: runId } = useParams();
const [params] = useSearchParams();
const module = params.get('module');
const run = useRun(runId ?? '', !!runId);
const context: ChatContext = { run_id: runId ?? null, module: module ?? run.data?.modules?.[0] ?? undefined };
const label = runId ? `${run.data ? runTitle(run.data) : 'this test run'} · this test run` : module ? (MODULE_NAMES[module] ?? module) : 'All areas';
return { runId: runId ?? null, context, label, narrowed: !!runId || !!module };
}
/** Who is answering: the coworker's face (moving while it works), its name and role. Decorative for screen readers,
* which hear the answer itself. */
function SpeakerRow({ who, working }: { who: Speaker; working: boolean }) {
return (