import Anthropic from '@anthropic-ai/sdk';
import { tools as allTools, handleToolCall } from './tools.js';

const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });

const SYSTEM_PROMPT = `You are a live demo assistant for SquareMCP — an AI Social Media Gateway that lets AI agents post to social platforms via the Model Context Protocol (MCP).

You have real tools connected to live platforms. When a visitor asks "can it send a Slack message?" or "show me how it works" — actually do it. Send a demo message, read channels, show real results.

What SquareMCP does:
- Connects AI coding assistants to social platforms: LinkedIn, TikTok, WhatsApp, Instagram, Twitter/X, Facebook, Telegram, Discord, Slack, and Email
- Works with any MCP-compatible client: Claude Desktop, Claude Code, Cursor, Windsurf, opencode, Codex CLI
- Provides a multi-tenant SaaS platform where each customer securely stores their own platform credentials
- Plans: Free (100 calls/month), Pro, Business

When using tools:
- For demo sends (Slack, Telegram, Discord), send a short, friendly message that explains this is a live SquareMCP demo
- After a tool succeeds, explain what just happened and how easy it was
- If a tool fails (missing credentials), explain what credential the customer would set up instead
- Never expose tokens or internal IDs in your reply

Keep replies concise. If you don't know something, say so and suggest emailing support@squaremcp.com.`;

// Tools the public demo agent is allowed to use (no customer auth needed — uses env var creds)
const DEMO_TOOL_NAMES = new Set([
  'slack_get_me',
  'slack_get_channels',
  'slack_send_message',
  'slack_get_messages',
  'discord_get_me',
  'discord_get_guilds',
  'discord_send_message',
  'telegram_get_me',
  'telegram_send_message',
]);

const demoTools = allTools
  .filter(t => DEMO_TOOL_NAMES.has(t.name))
  .map(t => ({
    name: t.name,
    description: t.description,
    input_schema: t.inputSchema as Anthropic.Tool['input_schema'],
  }));

export interface ChatMessage {
  role: 'user' | 'assistant';
  content: string;
}

export interface ChatResult {
  reply: string;
  toolsUsed: string[];
}

export async function handleChat(messages: ChatMessage[]): Promise<ChatResult> {
  if (!process.env.ANTHROPIC_API_KEY) {
    throw new Error('ANTHROPIC_API_KEY not configured');
  }

  const apiMessages: Anthropic.MessageParam[] = messages.map(m => ({
    role: m.role,
    content: m.content,
  }));

  const toolsUsed: string[] = [];
  const MAX_ITERATIONS = 5;

  for (let i = 0; i < MAX_ITERATIONS; i++) {
    const response = await client.messages.create({
      model: 'claude-haiku-4-5-20251001',
      max_tokens: 1024,
      system: SYSTEM_PROMPT,
      tools: demoTools,
      messages: apiMessages,
    });

    if (response.stop_reason === 'end_turn') {
      const text = response.content.find(b => b.type === 'text');
      return { reply: text?.text ?? 'Done.', toolsUsed };
    }

    if (response.stop_reason === 'tool_use') {
      // Add Claude's response (with tool_use blocks) to the message history
      apiMessages.push({ role: 'assistant', content: response.content });

      // Execute each tool call and collect results
      const toolResults: Anthropic.ToolResultBlockParam[] = [];
      for (const block of response.content) {
        if (block.type !== 'tool_use') continue;
        toolsUsed.push(block.name);
        try {
          const result = await handleToolCall(
            block.name,
            block.input as Record<string, unknown>,
            undefined // no customer — uses env var credentials
          );
          toolResults.push({
            type: 'tool_result',
            tool_use_id: block.id,
            content: result.content[0]?.text ?? '',
            is_error: result.isError,
          });
        } catch (err) {
          toolResults.push({
            type: 'tool_result',
            tool_use_id: block.id,
            content: `Error: ${(err as Error).message}`,
            is_error: true,
          });
        }
      }

      apiMessages.push({ role: 'user', content: toolResults });
      continue;
    }

    // Unexpected stop reason — return whatever text we have
    const text = response.content.find(b => b.type === 'text');
    return { reply: text?.text ?? 'Something unexpected happened.', toolsUsed };
  }

  return { reply: 'I ran too many steps. Please try a simpler request.', toolsUsed };
}
