/**
 * Unified Twilio WhatsApp Webhook
 *
 * Multi-tenant Twilio webhook that routes messages through the sector-aware
 * WhatsApp AI engine. All non-real-estate sectors flow through here.
 *
 * POST - Incoming messages (form-encoded from Twilio)
 * GET  - Health check
 *
 * Security:
 * - Rate limiting via Upstash Redis (100 req/min per IP)
 * - Twilio request signature validation per-org
 * - Input sanitization via DOMPurify
 * - Multi-tenant router with cache poisoning protection
 */

import { NextRequest, NextResponse } from "next/server";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { logger } from "@/lib/logger";
import { twilioWhatsAppService } from "@/lib/services/twilio-whatsapp.service";
import { whatsAppAIEngine } from "@/lib/services/whatsapp-ai-engine.service";
import { whatsAppRouter } from "@/lib/services/whatsapp-router.service";
import { getSectorConfigForIndustry } from "@/lib/config/sector-registry";
import { analyzeConversationTurn } from "@/lib/services/conversation-learning.service";
import { prisma } from "@/lib/db";
import {
  sanitizeChatMessage,
  sanitizePhoneNumber,
} from "@/lib/sanitizers/chat.sanitizer";
import type {
  ConversationContext,
  BookingFlowState,
} from "@/lib/types/twilio-whatsapp.types";
import {
  getLanguagePreference,
  setLanguagePreference,
} from "@/lib/services/whatsapp-language.service";
import { qatariDialectService } from "@/lib/services/qatari-dialect.service";

// =============================================================================
// Rate Limiter
// =============================================================================

let webhookRateLimit: Ratelimit | null = null;

if (
  process.env.UPSTASH_REDIS_REST_URL &&
  process.env.UPSTASH_REDIS_REST_TOKEN
) {
  const redis = new Redis({
    url: process.env.UPSTASH_REDIS_REST_URL,
    token: process.env.UPSTASH_REDIS_REST_TOKEN,
  });

  webhookRateLimit = new Ratelimit({
    redis,
    limiter: Ratelimit.slidingWindow(100, "1 m"),
    analytics: true,
    prefix: "webhook-twilio-whatsapp",
  });
}

// =============================================================================
// Arabic detection helper
// =============================================================================

const ARABIC_RANGE = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF]/;

function detectLanguage(text: string): "en" | "ar" {
  const arabicChars = (text.match(new RegExp(ARABIC_RANGE.source, "g")) || [])
    .length;
  return arabicChars > text.length * 0.3 ? "ar" : "en";
}

// =============================================================================
// GET - Health check
// =============================================================================

export async function GET(): Promise<Response> {
  return NextResponse.json({
    status: "ok",
    service: "twilio-whatsapp-webhook",
    timestamp: new Date().toISOString(),
  });
}

// =============================================================================
// POST - Incoming Twilio WhatsApp messages (all sectors)
// =============================================================================

export async function POST(request: NextRequest): Promise<Response> {
  try {
    // ── Rate limit ────────────────────────────────────────
    if (webhookRateLimit) {
      const ip =
        request.ip ??
        request.headers.get("x-forwarded-for") ??
        request.headers.get("x-real-ip") ??
        "unknown";

      const { success, limit, remaining, reset } =
        await webhookRateLimit.limit(ip);

      if (!success) {
        logger.security("Twilio webhook rate limit exceeded", {
          ip,
          limit,
          remaining,
          resetAt: new Date(reset).toISOString(),
          endpoint: request.url,
        });

        return NextResponse.json(
          { error: "Too many requests", retryAfter: reset },
          {
            status: 429,
            headers: {
              "X-RateLimit-Limit": limit.toString(),
              "X-RateLimit-Remaining": remaining.toString(),
              "X-RateLimit-Reset": reset.toString(),
              "Retry-After": Math.ceil((reset - Date.now()) / 1000).toString(),
            },
          },
        );
      }
    }

    // ── Parse form body ───────────────────────────────────
    const rawBody = await request.text();
    const params = new URLSearchParams(rawBody);

    // ── Twilio signature ──────────────────────────────────
    const signature = request.headers.get("x-twilio-signature");
    if (!signature) {
      logger.warn("Twilio webhook missing signature header");
      return new Response("", { status: 200 });
    }

    // ── Resolve organization from To number via unified router ───
    const toRaw = params.get("To") || "";
    const toNumber = toRaw.replace(/^whatsapp:/, "");

    const org = await whatsAppRouter.resolveOrganizationByTwilioNumber(toNumber);
    if (!org) {
      logger.warn("Twilio webhook: unknown To number", { toNumber });
      return new Response("", { status: 200 });
    }

    // ── Get Twilio credentials & validate signature ──────────────
    const credentials = await twilioWhatsAppService.getCredentialsForOrg(
      org.id,
    );
    if (!credentials) {
      logger.error("Twilio webhook: credentials not found", {
        organizationId: org.id,
      });
      return new Response("", { status: 200 });
    }

    const forwardedHost = request.headers.get("x-forwarded-host");
    const forwardedProto = request.headers.get("x-forwarded-proto") || "https";
    const ngrokUrl = process.env.NGROK_URL?.replace(/\/+$/, "");
    const fullUrl = forwardedHost
      ? `${forwardedProto}://${forwardedHost}/api/webhooks/twilio-whatsapp`
      : ngrokUrl
        ? `${ngrokUrl}/api/webhooks/twilio-whatsapp`
        : request.url;

    const isValid = twilioWhatsAppService.validateSignature(
      credentials.authToken,
      signature,
      fullUrl,
      Object.fromEntries(params),
    );

    if (!isValid) {
      logger.warn("Twilio webhook: signature validation failed", {
        organizationId: org.id,
        validationUrl: fullUrl,
      });
      return new Response("", { status: 200 });
    }

    // ── Load sector config ────────────────────────────────
    const sectorConfig = getSectorConfigForIndustry(org.industry ?? "");

    // ── Parse & sanitize message ──────────────────────────
    const message = twilioWhatsAppService.parseIncomingMessage(params);
    const body = sanitizeChatMessage(message.body);
    const phone = sanitizePhoneNumber(message.from.replace(/^whatsapp:/, ""));

    if (!phone) {
      logger.warn("Twilio webhook: empty phone after sanitization", {
        organizationId: org.id,
      });
      return new Response("", { status: 200 });
    }

    if (!body) {
      logger.debug("Twilio webhook: empty message body after sanitization", {
        organizationId: org.id,
        from: phone,
      });
      return new Response("", { status: 200 });
    }

    let language = detectLanguage(body);

    // ── Upsert conversation ───────────────────────────────
    let conversation = await prisma.org_whatsapp_conversations.findFirst({
      where: { organizationId: org.id, userPhone: phone },
    });

    const isNewConversation = !conversation;

    if (!conversation) {
      conversation = await prisma.org_whatsapp_conversations.create({
        data: {
          organizationId: org.id,
          userPhone: phone,
          userName: sanitizeChatMessage(message.profileName || "") || null,
          language,
          status: "active",
          lastMessageAt: new Date(),
          messageCount: 0,
          updatedAt: new Date(),
        },
      });
    }

    // ── Override language with stored preference ───────────
    let storedLanguagePreference: string | null = null;
    try {
      storedLanguagePreference = await getLanguagePreference(org.id, phone);
      if (
        storedLanguagePreference === "en" ||
        storedLanguagePreference === "ar"
      ) {
        language = storedLanguagePreference;
      }
    } catch (err) {
      logger.warn(
        "Failed to fetch language preference, falling back to detection",
        {
          organizationId: org.id,
          error: err instanceof Error ? err.message : "Unknown error",
        },
      );
    }

    // ── Bypass selecting_language if stored preference exists ──
    const conversationStateRaw =
      (conversation.conversationState as BookingFlowState | null) ?? null;
    let bypassedLanguageSelection = false;

    if (
      storedLanguagePreference &&
      conversationStateRaw?.phase === "selecting_language"
    ) {
      await prisma.org_whatsapp_conversations.update({
        where: { id: conversation.id },
        data: {
          conversationState: JSON.parse(JSON.stringify({ phase: "idle" })),
          language,
          updatedAt: new Date(),
        },
      });
      bypassedLanguageSelection = true;
    }

    // ── First-message bilingual greeting ──────────────────
    if (isNewConversation && !storedLanguagePreference) {
      await prisma.org_whatsapp_messages.create({
        data: {
          conversationId: conversation.id,
          role: "user",
          content: body,
          language,
          timestamp: new Date(),
        },
      });

      const bilingualGreeting =
        `هلا! 👋 مشكور على تواصلك مع ${org.name}.\n\n` +
        `Hello! 👋 Thank you for contacting ${org.name}.\n\n` +
        `اختر لغتك المفضلة / Please select your preferred language:\n` +
        `1️⃣ العربية (Arabic)\n` +
        `2️⃣ English`;

      await twilioWhatsAppService.sendTextMessage(
        credentials,
        message.from,
        bilingualGreeting,
      );

      await prisma.org_whatsapp_messages.create({
        data: {
          conversationId: conversation.id,
          role: "assistant",
          content: bilingualGreeting,
          language: "ar",
          timestamp: new Date(),
        },
      });

      await prisma.org_whatsapp_conversations.update({
        where: { id: conversation.id },
        data: {
          conversationState: JSON.parse(
            JSON.stringify({ phase: "selecting_language" }),
          ),
          lastMessageAt: new Date(),
          messageCount: { increment: 1 },
          updatedAt: new Date(),
        },
      });

      logger.info("Twilio WhatsApp: sent bilingual greeting to new user", {
        organizationId: org.id,
        conversationId: conversation.id,
      });

      return new Response("", { status: 200 });
    }

    // ── Language selection handler ──────────────────────────
    const conversationState = conversationStateRaw;

    if (
      !bypassedLanguageSelection &&
      conversationState?.phase === "selecting_language"
    ) {
      await prisma.org_whatsapp_messages.create({
        data: {
          conversationId: conversation.id,
          role: "user",
          content: body,
          language,
          timestamp: new Date(),
        },
      });

      const normalized = body.toLowerCase().trim();
      const arabicChars = (
        body.match(new RegExp(ARABIC_RANGE.source, "g")) || []
      ).length;
      const isArabicChoice =
        normalized === "1" ||
        normalized === "arabic" ||
        normalized === "عربي" ||
        normalized === "عربية" ||
        arabicChars > body.length * 0.5;
      const isEnglishChoice =
        normalized === "2" || normalized === "english" || normalized === "eng";

      if (!isArabicChoice && !isEnglishChoice) {
        const reprompt =
          `لو سمحت اختر رقم 1 أو 2 👇\n` +
          `Please reply with 1 or 2 👇\n\n` +
          `1️⃣ العربية (Arabic)\n` +
          `2️⃣ English`;

        await twilioWhatsAppService.sendTextMessage(
          credentials,
          message.from,
          reprompt,
        );

        await prisma.org_whatsapp_messages.create({
          data: {
            conversationId: conversation.id,
            role: "assistant",
            content: reprompt,
            language: "ar",
            timestamp: new Date(),
          },
        });

        await prisma.org_whatsapp_conversations.update({
          where: { id: conversation.id },
          data: {
            lastMessageAt: new Date(),
            messageCount: { increment: 1 },
            updatedAt: new Date(),
          },
        });

        return new Response("", { status: 200 });
      }

      const selectedLanguage: "en" | "ar" = isArabicChoice ? "ar" : "en";

      try {
        await setLanguagePreference(org.id, phone, selectedLanguage);
      } catch (err) {
        logger.error("Failed to save language preference", {
          organizationId: org.id,
          error: err instanceof Error ? err.message : "Unknown error",
        });
      }

      await prisma.org_whatsapp_conversations.update({
        where: { id: conversation.id },
        data: {
          language: selectedLanguage,
          conversationState: JSON.parse(JSON.stringify({ phase: "idle" })),
          lastMessageAt: new Date(),
          messageCount: { increment: 1 },
          updatedAt: new Date(),
        },
      });

      const transactionNoun =
        sectorConfig.transactionNoun[selectedLanguage] ||
        (selectedLanguage === "ar" ? "حجز" : "booking");

      const confirmationText =
        selectedLanguage === "ar"
          ? `ممتاز! بنتواصل معاك بالعربي. شلون نقدر نساعدك اليوم؟ تبي تسوي ${transactionNoun}؟`
          : `Great! We'll communicate in English. How can we help you today? Would you like to make a ${transactionNoun}?`;

      await twilioWhatsAppService.sendTextMessage(
        credentials,
        message.from,
        confirmationText,
      );

      await prisma.org_whatsapp_messages.create({
        data: {
          conversationId: conversation.id,
          role: "assistant",
          content: confirmationText,
          language: selectedLanguage,
          timestamp: new Date(),
        },
      });

      logger.info("Twilio WhatsApp: language selected", {
        organizationId: org.id,
        conversationId: conversation.id,
        language: selectedLanguage,
      });

      return new Response("", { status: 200 });
    }

    // ── Human handoff check ─────────────────────────────────
    if (conversation.humanControlled) {
      const thirtyMinAgo = new Date(Date.now() - 30 * 60 * 1000);
      const stillActive =
        conversation.humanControlledAt &&
        new Date(conversation.humanControlledAt) > thirtyMinAgo;

      if (stillActive) {
        await prisma.org_whatsapp_messages.create({
          data: {
            conversationId: conversation.id,
            role: "user",
            content: body,
            language,
            timestamp: new Date(),
          },
        });

        await prisma.org_whatsapp_conversations.update({
          where: { id: conversation.id },
          data: {
            lastMessageAt: new Date(),
            messageCount: { increment: 1 },
            updatedAt: new Date(),
          },
        });

        logger.info("Twilio WhatsApp: human-controlled, skipping AI", {
          organizationId: org.id,
          conversationId: conversation.id,
        });

        return new Response("", { status: 200 });
      } else {
        await prisma.org_whatsapp_conversations.update({
          where: { id: conversation.id },
          data: {
            humanControlled: false,
            humanControlledAt: null,
            humanControlledBy: null,
            updatedAt: new Date(),
          },
        });
      }
    }

    // ── Load message history (last 20) ────────────────────
    const recentMessages = await prisma.org_whatsapp_messages.findMany({
      where: { conversationId: conversation.id },
      orderBy: { timestamp: "desc" },
      take: 20,
    });

    // ── Store inbound message ─────────────────────────────
    await prisma.org_whatsapp_messages.create({
      data: {
        conversationId: conversation.id,
        role: "user",
        content: body,
        language,
        timestamp: new Date(),
      },
    });

    // ── Build conversation context ────────────────────────
    const conversationHistory = (recentMessages as Array<{
      role: string;
      content: string;
      timestamp: Date;
    }>)
      .reverse()
      .map((m) => ({
        role: m.role as "user" | "assistant",
        content: m.content,
        timestamp: m.timestamp,
      }));

    const context: ConversationContext = {
      organizationId: org.id,
      organizationName: org.name,
      customerPhone: phone,
      customerName:
        sanitizeChatMessage(message.profileName || "") ||
        conversation.userName ||
        undefined,
      customerEmail: conversation.customerEmail || undefined,
      language,
      conversationHistory,
      conversationState:
        (conversation.conversationState as BookingFlowState | null) ?? null,
      isReturningCustomer: conversation.messageCount > 0,
      industry: org.industry ?? undefined,
      sector: org.sector ?? undefined,
    };

    // ── Process via unified sector-aware AI engine ──────────
    const result = await whatsAppAIEngine.processMessage(
      context,
      body,
      sectorConfig,
    );

    // ── Guard against empty AI response ─────────────────
    if (!result.responseText) {
      result.responseText =
        language === "ar"
          ? `أنا هنا لمساعدتك بخدمات ${org.name}. شلون أقدر أساعدك؟`
          : `I'm here to help with ${org.name} services. How can I assist you?`;
    }

    // ── Qatari dialect post-processing ──────────────────
    if (language === "ar") {
      try {
        // Extract URL-containing lines before translation so the Qatari
        // dialect LLM doesn't strip or rewrite them
        const urlLineRegex = /^.*https?:\/\/\S+.*$/gm;
        const linkLines = result.responseText.match(urlLineRegex) || [];
        const textForTranslation =
          linkLines.length > 0
            ? result.responseText
                .replace(urlLineRegex, "")
                .replace(/\n{3,}/g, "\n\n")
                .trim()
            : result.responseText;

        const qatariResult =
          await qatariDialectService.translateToQatari(textForTranslation);
        if (qatariResult.translation) {
          result.responseText =
            linkLines.length > 0
              ? qatariResult.translation + "\n\n" + linkLines.join("\n")
              : qatariResult.translation;
        }
      } catch (err) {
        logger.warn("Qatari dialect translation failed, using original", {
          organizationId: org.id,
          error: err instanceof Error ? err.message : "Unknown error",
        });
      }
    }

    // ── Store outbound message with KB metadata ─────────────
    await prisma.org_whatsapp_messages.create({
      data: {
        conversationId: conversation.id,
        role: "assistant",
        content: result.responseText,
        language,
        intent: result.intent,
        kbUsed: result.kbUsed ?? false,
        kbConfidence: result.kbConfidence ?? 0,
        escalatedToHuman: result.escalatedToHuman ?? false,
        timestamp: new Date(),
      },
    });

    // ── Update conversation state ─────────────────────────
    await prisma.org_whatsapp_conversations.update({
      where: { id: conversation.id },
      data: {
        lastMessageAt: new Date(),
        messageCount: { increment: 1 },
        ...(result.newState
          ? { conversationState: JSON.parse(JSON.stringify(result.newState)) }
          : {}),
        updatedAt: new Date(),
      },
    });

    // ── Send response via Twilio ──────────────────────────
    const sendResult = await twilioWhatsAppService.sendTextMessage(
      credentials,
      message.from,
      result.responseText,
    );

    if (!sendResult.success) {
      logger.error("Failed to send Twilio WhatsApp response", {
        organizationId: org.id,
        error: sendResult.error,
      });
    }

    // ── Non-blocking: track for learning loop ─────────────
    void analyzeConversationTurn({
      organizationId: org.id,
      sector: org.sector ?? undefined,
      language,
      customerQuestion: body,
      kbUsed: result.kbUsed ?? false,
      kbConfidence: result.kbConfidence ?? 0,
      intent: result.intent,
      escalatedToHuman: result.escalatedToHuman ?? false,
    }).catch((err) => {
      logger.error("Conversation learning tracking failed", { error: err });
    });

    logger.info("Twilio WhatsApp message processed", {
      organizationId: org.id,
      sector: org.sector,
      intent: result.intent,
      phone: phone.slice(-4),
      messageSid: message.messageSid,
    });

    // Twilio expects empty 200
    return new Response("", { status: 200 });
  } catch (error) {
    logger.error("Twilio WhatsApp webhook error", { error });
    return new Response("", { status: 200 });
  }
}
