import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import { sendAgentMessage } from '@server/integrations/ai/restaurantChatAgent';
import { db } from '@server/db/drizzle';
import { sql } from 'drizzle-orm';
import { initDatabase } from '@server/db/init';

interface ChatAgentRow {
  system_prompt: string | null;
  greeting_script: string | null;
  closing_script: string | null;
  fallback_rules: Array<{ trigger: string; action: string; priority: string }> | null;
  model_id: string | null;
}

export const POST = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    const { conversationId, message, history } = await req.json();
    const { restaurantId } = req.session;

    if (!restaurantId || !conversationId || !message) {
      return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
    }

    await initDatabase();

    const [chatAgentResult, totalAgentsResult, legacyConfigResult, menuResult, restaurantResult] = await Promise.all([
      /* raw: SELECT a.system_prompt, ... FROM ai_agents a LEFT JOIN llm_models lm ... WHERE a.restaurant_id = $1 ... */
      db.execute(sql`
        SELECT a.system_prompt, a.greeting_script, a.closing_script, a.fallback_rules,
                lm.model_id
         FROM ai_agents a
         LEFT JOIN llm_models lm ON lm.id = a.llm_model_id
         WHERE a.restaurant_id = ${restaurantId}
           AND a.is_active = true
           AND 'chat' = ANY(a.channels)
         ORDER BY a.is_default DESC, a.created_at
         LIMIT 1
      `),
      db.execute(sql`SELECT COUNT(*) AS count FROM ai_agents WHERE restaurant_id = ${restaurantId} AND is_active = true`),
      db.execute(sql`SELECT * FROM ai_agent_configs WHERE restaurant_id = ${restaurantId} LIMIT 1`),
      db.execute(sql`SELECT name, price, description FROM menu_items WHERE restaurant_id = ${restaurantId} AND is_available = true LIMIT 60`),
      db.execute(sql`SELECT name FROM restaurants WHERE id = ${restaurantId} LIMIT 1`),
    ]);

    const chatAgentRow = chatAgentResult.rows[0] as unknown as ChatAgentRow | undefined;
    const totalAgentsRow = totalAgentsResult.rows[0] as { count: string } | undefined;
    const legacyConfig = legacyConfigResult.rows[0] as Record<string, unknown> | undefined;
    const hasAnyAgents = parseInt(totalAgentsRow?.count ?? '0', 10) > 0;

    let agentConfig: {
      model: string;
      systemPrompt?: string;
      responseStyle?: string;
      greetingScript?: string;
      closingScript?: string;
      fallbackRules?: Array<{ trigger: string; action: string; priority: string }>;
    } | null = null;

    if (chatAgentRow) {
      agentConfig = {
        model: chatAgentRow.model_id || (legacyConfig?.model as string) || 'gpt-4o',
        systemPrompt: chatAgentRow.system_prompt ?? (legacyConfig?.system_prompt as string),
        responseStyle: (legacyConfig?.response_style as string) || 'friendly',
        greetingScript: chatAgentRow.greeting_script ?? (legacyConfig?.greeting_script as string),
        closingScript: chatAgentRow.closing_script ?? (legacyConfig?.closing_script as string),
        fallbackRules: Array.isArray(chatAgentRow.fallback_rules)
          ? chatAgentRow.fallback_rules
          : (Array.isArray(legacyConfig?.fallback_rules) ? legacyConfig.fallback_rules as Array<{ trigger: string; action: string; priority: string }> : []),
      };
    } else if (!hasAnyAgents && legacyConfig) {
      agentConfig = {
        model: (legacyConfig.model as string) || 'gpt-4o',
        systemPrompt: legacyConfig.system_prompt as string,
        responseStyle: (legacyConfig.response_style as string) || 'friendly',
        greetingScript: legacyConfig.greeting_script as string,
        closingScript: legacyConfig.closing_script as string,
        fallbackRules: Array.isArray(legacyConfig.fallback_rules) ? legacyConfig.fallback_rules as Array<{ trigger: string; action: string; priority: string }> : [],
      };
    }

    if (!agentConfig) {
      return NextResponse.json({
        reply: "I'm sorry, chat is not available at the moment. Please contact us by phone or try again later.",
        shouldEscalate: false,
        escalationReason: undefined,
        detectedIntent: 'general',
      });
    }

    interface MenuItemRow { name: string; price: number; description: string }
    const menuContext = {
      items: (menuResult.rows as unknown as MenuItemRow[]).map(r => ({ name: r.name, price: r.price, description: r.description })),
    };

    const restaurantName = (restaurantResult.rows[0] as { name: string } | undefined)?.name || 'Our Restaurant';

    const chatHistory = Array.isArray(history) ? history : [];

    const agentResponse = await sendAgentMessage(chatHistory, agentConfig, restaurantName, menuContext, restaurantId!);

    return NextResponse.json({
      reply: agentResponse.content,
      shouldEscalate: agentResponse.shouldEscalate,
      escalationReason: agentResponse.escalationReason,
      detectedIntent: agentResponse.detectedIntent,
    });
  })
);
