import React, { useState, useCallback, useRef, useEffect } from 'react';
import { Box, Text, useApp, useInput } from 'ink';
import { StatusBar } from './StatusBar.js';
import { MessageList } from './MessageList.js';
import { InputBar } from './InputBar.js';
import { parseCommand, getHelpText, resolveModelAlias, getCommandCompletions } from '../commands.js';
import type { Message, ModelClient, TokenUsage } from '../../../models/types.js';
import type { ModelRouter } from '../../../models/router.js';
import type { ManagedSession } from '../../../session/index.js';
import type { NativeAgent, ToolUseEvent } from '../../../backends/native/agent.js';
import type { HookEngine, HookResult } from '../../../hooks/index.js';
import type { ModelConfig, ModelProvider } from '../../../config/schema.js';
import { MODEL_PROVIDERS } from '../../../config/schema.js';
import { createClientFromConfig } from '../../../daemon/index.js';

/** Format a tool name like "gmail.list" -> "Gmail: List" */
function formatToolName(name: string): string {
  const parts = name.split('.');
  return parts.map((p, i) => {
    const capitalized = p.charAt(0).toUpperCase() + p.slice(1);
    return i === 0 && parts.length > 1 ? capitalized + ':' : capitalized;
  }).join(' ');
}

/** Format tool args as a compact, readable summary. */
function formatToolArgs(args: unknown): string {
  if (!args || typeof args !== 'object') {return '';}
  const entries = Object.entries(args as Record<string, unknown>);
  if (entries.length === 0) {return '';}
  const parts = entries.map(([key, value]) => {
    if (typeof value === 'string') {
      const display = value.length > 50 ? value.slice(0, 47) + '...' : value;
      return `${key}: "${display}"`;
    }
    if (typeof value === 'number' || typeof value === 'boolean') {
      return `${key}: ${value}`;
    }
    return `${key}: ${JSON.stringify(value)}`;
  });
  return parts.join(', ');
}

export interface AppProps {
  session: ManagedSession;
  modelClient: ModelClient;
  modelRouter?: ModelRouter;
  systemPrompt: string;
  model: string;
  agent?: NativeAgent;
  hookEngine?: HookEngine;
  modelProviderConfigs?: Partial<Record<ModelProvider, ModelConfig>>;
  onExit?: () => void;
}

export function App({
  session,
  modelClient,
  modelRouter,
  systemPrompt,
  model,
  agent,
  hookEngine,
  modelProviderConfigs,
  onExit,
}: AppProps): React.ReactElement {
  const { exit } = useApp();
  const [input, setInput] = useState('');
  const [messages, setMessages] = useState<Message[]>(session.getHistory());
  const [isStreaming, setIsStreaming] = useState(false);
  const [streamingContent, setStreamingContent] = useState('');
  const [scrollOffset, setScrollOffset] = useState(0);
  const [tokenUsage, setTokenUsage] = useState<TokenUsage>({ inputTokens: 0, outputTokens: 0 });
  const [currentModel, setCurrentModel] = useState(() => {
    if (!modelRouter) {return model;}
    return modelRouter.getLabel(modelRouter.getTier());
  });

  const abortRef = useRef(false);
  const toolLinesRef = useRef<string[]>([]);

  const confirmResolveRef = useRef<((result: HookResult) => void) | null>(null);
  const [confirmation, setConfirmation] = useState<{ tool: string; args: Record<string, unknown> } | null>(null);

  // Set up an Ink-compatible onToolUse callback for the agent.
  // This replaces process.stdout writes (which corrupt Ink rendering)
  // with one that updates React state to show tool activity.
  useEffect(() => {
    if (!agent) {return;}

    const handleToolEvent = (event: ToolUseEvent) => {
      if (event.type === 'start') {
        const label = formatToolName(event.tool);
        const argsStr = event.args ? ` (${formatToolArgs(event.args)})` : '';
        toolLinesRef.current = [...toolLinesRef.current, `> ${label}${argsStr}`];
        setStreamingContent(toolLinesRef.current.join('\n'));
        return;
      }

      if (event.type === 'end' && event.result) {
        const icon = event.result.success ? 'done' : 'error';
        const detail = event.result.success
          ? `(${event.result.output.split('\n').length} lines)`
          : (event.result.error ?? 'unknown error');
        toolLinesRef.current = [...toolLinesRef.current, `  ${icon} ${detail}`];
        setStreamingContent(toolLinesRef.current.join('\n'));
      }
    };

    agent.setOnToolUse(handleToolEvent);
    return () => {
      agent.setOnToolUse(undefined);
    };
  }, [agent]);

  // Inline confirmations for dangerous tools (e.g. shell.exec) in fullscreen mode.
  useEffect(() => {
    if (!hookEngine) {return;}

    hookEngine.setInteractiveConfirmer(async (pending) => {
      return await new Promise<HookResult>((resolve) => {
        confirmResolveRef.current = resolve;
        setConfirmation({ tool: pending.tool, args: pending.args });
      });
    });

    return () => {
      hookEngine.setInteractiveConfirmer(undefined);
      confirmResolveRef.current = null;
      setConfirmation(null);
    };
  }, [hookEngine]);

  useInput((inputChar, key) => {
    // Confirmation prompt mode: capture y/n and ignore everything else.
    if (confirmation && confirmResolveRef.current) {
      const c = inputChar.toLowerCase();
      if (c === 'y') {
        confirmResolveRef.current({ approved: true });
        confirmResolveRef.current = null;
        setConfirmation(null);
        return;
      }
      if (c === 'n') {
        confirmResolveRef.current({ approved: false, reason: 'Denied by user' });
        confirmResolveRef.current = null;
        setConfirmation(null);
        return;
      }
      return;
    }

    if (key.escape) {
      if (isStreaming) {
        abortRef.current = true;
      } else {
        onExit?.();
        exit();
      }
      return;
    }

    // Tab completion for slash commands
    if (key.tab && !isStreaming) {
      const completions = getCommandCompletions(input);
      if (completions.length > 0) {
        setInput(completions[0]);
        return;
      }
    }

    // Only handle scroll keys, ignore regular typing
    if (!key.upArrow && !key.downArrow && !key.pageUp && !key.pageDown && !key.tab) {
      return;
    }

    if (key.upArrow && scrollOffset > 0) {
      setScrollOffset(prev => Math.max(0, prev - 1));
    }
    if (key.downArrow) {
      setScrollOffset(prev => Math.min(messages.length - 1, prev + 1));
    }
    if (key.pageUp) {
      setScrollOffset(prev => Math.max(0, prev - 10));
    }
    if (key.pageDown) {
      setScrollOffset(prev => Math.min(messages.length - 1, prev + 10));
    }
  });

  const handleSubmit = useCallback(async (value: string) => {
    if (confirmation) {
      return;
    }

    const command = parseCommand(value);
    if (!command) {return;}

    setInput('');

    switch (command.type) {
      case 'quit':
        onExit?.();
        exit();
        return;

      case 'reset':
        if (agent) {
          agent.reset();
        } else {
          session.clear();
        }
        setMessages([]);
        setTokenUsage({ inputTokens: 0, outputTokens: 0 });
        setScrollOffset(0);
        return;

      case 'help': {
        const helpMsg: Message = { role: 'assistant', content: getHelpText() };
        setMessages(prev => [...prev, session.addMessage(helpMsg)]);
        return;
      }

      case 'status': {
        const status = `Session: ${session.id}\nMessages: ${messages.length}\nTokens: ${tokenUsage.inputTokens} in / ${tokenUsage.outputTokens} out`;
        const statusMsg: Message = { role: 'assistant', content: status };
        setMessages(prev => [...prev, session.addMessage(statusMsg)]);
        return;
      }

      case 'model': {
        if (!modelRouter) {
          setMessages(prev => [...prev, session.addMessage({ role: 'assistant', content: 'Model switching not available.' })]);
          return;
        }

        // /model
        if (!command.name) {
          const current = modelRouter.getTier();
          const available = modelRouter.getAvailableTiers();
          const labels = modelRouter.getAllLabels();

          const lines: string[] = [];
          lines.push(`Active tier: ${current}`);
          for (const t of available) {
            const label = labels[t] ?? 'unknown';
            const strict = modelRouter.isTierStrict(t) ? ' (strict)' : '';
            lines.push(`  ${t}: ${label}${strict}${t === current ? ' ←' : ''}`);
          }

          setMessages(prev => [...prev, session.addMessage({ role: 'assistant', content: lines.join('\n') })]);
          return;
        }

        // /model <tier> <provider/model>
        if (command.providerModel) {
          const tier = resolveModelAlias(command.name);
          const providerModel = command.providerModel;

          const slashIdx = providerModel.indexOf('/');
          if (slashIdx === -1) {
            setMessages(prev => [...prev, session.addMessage({
              role: 'assistant',
              content: 'Invalid format. Use provider/model (e.g. anthropic/claude-sonnet-4)',
            })]);
            return;
          }

          const provider = providerModel.slice(0, slashIdx);
          const modelName = providerModel.slice(slashIdx + 1);

          if (!MODEL_PROVIDERS.includes(provider as ModelProvider)) {
            setMessages(prev => [...prev, session.addMessage({
              role: 'assistant',
              content: `Unknown provider "${provider}". Known providers: ${MODEL_PROVIDERS.join(', ')}`,
            })]);
            return;
          }

          try {
            const providerType = provider as ModelProvider;
            const template = modelProviderConfigs?.[providerType];
            const client = createClientFromConfig({
              ...(template ?? {}),
              provider: providerType,
              model: modelName,
            });

            modelRouter.setClient(tier, client, providerModel);
            modelRouter.setTierStrict(tier, true);

            if (agent && tier === modelRouter.getTier()) {
              agent.setModelTier(tier);
              setCurrentModel(modelRouter.getLabel(tier));
            }

            setMessages(prev => [...prev, session.addMessage({
              role: 'assistant',
              content: `Set ${tier} to: ${providerModel}\nFallbacks for ${tier} disabled (strict tier mode).`,
            })]);
          } catch (error) {
            const msg = error instanceof Error ? error.message : String(error);
            setMessages(prev => [...prev, session.addMessage({ role: 'assistant', content: `Failed to create client: ${msg}` })]);
          }

          return;
        }

        // /model <tier>
        const tier = resolveModelAlias(command.name);
        if (modelRouter.setTier(tier)) {
          if (agent) {
            agent.setModelTier(tier);
          }
          setCurrentModel(modelRouter.getLabel(tier));
          setMessages(prev => [...prev, session.addMessage({ role: 'assistant', content: `Switched to model: ${tier}` })]);
        } else {
          setMessages(prev => [...prev, session.addMessage({ role: 'assistant', content: `Model not available: ${command.name}` })]);
        }
        return;
      }

      case 'fullscreen':
        return;

      case 'transfer':
        setMessages(prev => [...prev, session.addMessage({ role: 'assistant', content: 'Transfer not supported in fullscreen mode.' })]);
        return;

      case 'message':
        break;
    }

    if (command.type !== 'message' || isStreaming) {
      return;
    }

    // Add user message to UI (and session if no agent — agent adds it internally)
    const userMessage: Message = { role: 'user', content: command.content };
    if (!agent) {
      const messageWithTimestamp = session.addMessage(userMessage);
      setMessages(prev => [...prev, messageWithTimestamp]);
    } else {
      setMessages(prev => [...prev, { ...userMessage, timestamp: Date.now() }]);
    }
    setScrollOffset(0);

    setIsStreaming(true);
    setStreamingContent('');
    toolLinesRef.current = [];
    abortRef.current = false;

    try {
      if (agent) {
        await agent.process(command.content);
        const usage = agent.getUsage();
        setTokenUsage({ inputTokens: usage.inputTokens, outputTokens: usage.outputTokens });
        setMessages(session.getHistory());
      } else if (modelClient.chatStream) {
        let fullContent = '';

        for await (const event of modelClient.chatStream({
          messages: session.getHistory(),
          system: systemPrompt,
        })) {
          if (abortRef.current) {
            fullContent += '\n\n[interrupted]';
            break;
          }

          if (event.type === 'content' && event.content) {
            fullContent += event.content;
            setStreamingContent(fullContent);
          }
          if (event.type === 'done' && event.usage) {
            setTokenUsage(prev => ({
              inputTokens: prev.inputTokens + event.usage!.inputTokens,
              outputTokens: prev.outputTokens + event.usage!.outputTokens,
            }));
          }
          if (event.type === 'error') {
            throw event.error ?? new Error('Stream error');
          }
        }

        const assistantMessage: Message = { role: 'assistant', content: fullContent };
        setMessages(prev => [...prev, session.addMessage(assistantMessage)]);
      } else {
        const response = await modelClient.chat({
          messages: session.getHistory(),
          system: systemPrompt,
        });

        setTokenUsage(prev => ({
          inputTokens: prev.inputTokens + response.usage.inputTokens,
          outputTokens: prev.outputTokens + response.usage.outputTokens,
        }));

        const assistantMessage: Message = { role: 'assistant', content: response.content };
        setMessages(prev => [...prev, session.addMessage(assistantMessage)]);
      }
    } catch (error) {
      const msg = error instanceof Error ? error.message : 'Unknown error';
      setMessages(prev => [...prev, session.addMessage({ role: 'assistant', content: `Error: ${msg}` })]);
    } finally {
      setIsStreaming(false);
      setStreamingContent('');
    }
  }, [
    confirmation,
    session,
    agent,
    modelClient,
    modelRouter,
    systemPrompt,
    exit,
    onExit,
    isStreaming,
    messages.length,
    tokenUsage.inputTokens,
    tokenUsage.outputTokens,
    modelProviderConfigs,
  ]);

  return (
    <Box flexDirection="column" height="100%">
      <MessageList
        messages={messages}
        scrollOffset={scrollOffset}
        streamingContent={isStreaming ? streamingContent : undefined}
      />

      {confirmation ? (
        <Box paddingX={1} paddingY={0} borderStyle="round" borderColor="yellow">
          <Text color="yellow">
            Confirmation required: {confirmation.tool}{' '}
            {Object.keys(confirmation.args).length > 0 ? JSON.stringify(confirmation.args) : ''}
          </Text>
          <Text color="yellow">Press y to approve, n to deny.</Text>
        </Box>
      ) : null}

      <InputBar
        value={input}
        onChange={setInput}
        onSubmit={handleSubmit}
        isLoading={isStreaming || !!confirmation}
        placeholder={confirmation
          ? 'Confirmation required (press y/n)'
          : (isStreaming ? 'Flynn is typing... (Esc to cancel)' : 'Type a message... (Esc=exit, /help)')}
      />

      <StatusBar
        sessionId={session.id}
        messageCount={messages.length}
        model={currentModel}
        tokenUsage={{ input: tokenUsage.inputTokens, output: tokenUsage.outputTokens }}
        isStreaming={isStreaming}
      />
    </Box>
  );
}
