import React, { useState, useMemo, useCallback } from "react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { __ } from "../lib/i18n";
import {
  deleteEmailSequence,
  fetchEmailLogs,
  fetchEmailSequences,
  updateEmailSequenceStatus,
} from "../api/email-automation-api";
import { useToast } from "../components/ui/toast";
import {
  Card,
  CardContent,
  CardFooter,
  CardHeader,
  CardTitle,
} from "../components/ui/card";
import { Button } from "../components/ui/button";
import { Badge } from "../components/ui/badge";
import { PageHeader } from "../components/common/PageHeader";
import { ConfirmationDialog } from "../components/ui/confirmation-dialog";
import { Modal } from "../components/ui/modal";
import { Alert } from "../components/ui/alert";
import { Label } from "../components/ui/label";
import PremiumUpgradeCard from "./premium-pages/EmailAutomation";
import {
  Mail,
  Edit,
  Clock,
  FileText,
  Bell,
  Plus,
  Trash2,
  Play,
  Pause,
  Zap,
  GitBranch,
  Loader2,
  Save,
  Settings2,
  Send,
} from "lucide-react";
import {
  isProPluginActive,
  isEmailAutomationModuleEnabled,
} from "../lib/plugin-utils";
import { useEmailSettingsManager } from "../hooks/useEmailSettingsManager";
import { EmailDeliverySection } from "../components/settings/EmailDeliverySection";
import { EmailTemplatesList } from "../components/email/EmailTemplatesList";
import { Pagination } from "../components/shared";

interface EmailLog {
  id: number;
  template_id?: number | null;
  template_key: string;
  sequence_id?: number | null;
  recipient_email: string;
  recipient_name: string;
  subject: string;
  // Full HTML body — only loaded when the detail modal asks for it
  // (the list query also returns it, but it can be large; the modal
  // is the only consumer of this field).
  body?: string;
  context_type?: string | null;
  context_id?: number | null;
  status: "sent" | "failed" | "opened" | "clicked";
  error_message?: string | null;
  // Server-side JSON-encoded — may arrive as a string OR a parsed object
  // depending on the response shape.
  metadata?: string | Record<string, unknown> | null;
  sent_at: string;
}

interface EmailSequence {
  id: number;
  name: string;
  description: string;
  trigger_type: string;
  trigger_config: any;
  status: "active" | "paused" | "draft";
  applicable_to: string;
  trip_ids: number[];
  steps_count?: number;
}

const EmailSequencesList: React.FC = () => {
  const queryClient = useQueryClient();
  const { showToast } = useToast();

  const { data: sequencesData, isLoading } = useQuery({
    queryKey: ["email-sequences"],
    queryFn: () => fetchEmailSequences(),
    enabled: isEmailAutomationModuleEnabled(),
  });

  const deleteMutation = useMutation({
    mutationFn: async (id: number) => {
      return await deleteEmailSequence(id);
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["email-sequences"] });
      showToast(__("Sequence deleted"), "success");
    },
  });

  const toggleStatusMutation = useMutation({
    mutationFn: async ({ id, status }: { id: number; status: string }) => {
      return await updateEmailSequenceStatus(id, status);
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["email-sequences"] });
      showToast(__("Sequence status updated"), "success");
    },
  });

  const sequences = (
    Array.isArray(sequencesData) ? sequencesData : []
  ) as EmailSequence[];

  const navigateToCreate = () => {
    window.location.href =
      "admin.php?page=yatra&subpage=email-automation&tab=sequence&action=create";
  };

  const navigateToEdit = (id: number) => {
    window.location.href = `admin.php?page=yatra&subpage=email-automation&tab=sequence&action=edit&id=${id}`;
  };

  if (isLoading) {
    return (
      <div className="space-y-6 animate-pulse">
        {/* Header Skeleton */}
        <div className="flex items-center justify-between">
          <div>
            <div className="h-6 w-36 bg-gray-200 dark:bg-gray-700 rounded mb-2"></div>
            <div className="h-4 w-64 bg-gray-200 dark:bg-gray-700 rounded"></div>
          </div>
          <div className="h-10 w-36 bg-gray-200 dark:bg-gray-700 rounded"></div>
        </div>

        {/* Sequences List Skeleton */}
        <div className="space-y-4">
          {[1, 2, 3].map((i) => (
            <Card key={i}>
              <CardContent className="p-4">
                <div className="flex items-center justify-between">
                  <div className="flex-1">
                    <div className="flex items-center gap-3 mb-2">
                      <div className="h-5 w-48 bg-gray-200 dark:bg-gray-700 rounded"></div>
                      <div className="h-5 w-16 bg-gray-200 dark:bg-gray-700 rounded"></div>
                    </div>
                    <div className="h-4 w-72 bg-gray-200 dark:bg-gray-700 rounded mb-2"></div>
                    <div className="flex items-center gap-4">
                      <div className="h-3 w-32 bg-gray-200 dark:bg-gray-700 rounded"></div>
                      <div className="h-3 w-20 bg-gray-200 dark:bg-gray-700 rounded"></div>
                    </div>
                  </div>
                  <div className="flex items-center gap-2">
                    <div className="h-8 w-8 bg-gray-200 dark:bg-gray-700 rounded"></div>
                    <div className="h-8 w-16 bg-gray-200 dark:bg-gray-700 rounded"></div>
                    <div className="h-8 w-8 bg-gray-200 dark:bg-gray-700 rounded"></div>
                  </div>
                </div>
              </CardContent>
            </Card>
          ))}
        </div>
      </div>
    );
  }

  const getStatusBadge = (status: string) => {
    switch (status) {
      case "active":
        return (
          <Badge className="bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400">
            {__("Active")}
          </Badge>
        );
      case "paused":
        return (
          <Badge className="bg-yellow-100 text-yellow-700 dark:bg-yellow-900/30 dark:text-yellow-400">
            {__("Paused")}
          </Badge>
        );
      default:
        return (
          <Badge className="bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-400">
            {__("Draft")}
          </Badge>
        );
    }
  };

  const emailEvents = (window as any).yatraAdmin?.emailEvents || [];

  const getTriggerLabel = (triggerType: string) => {
    const ev = emailEvents.find((e: { key?: string }) => e.key === triggerType);
    return ev?.name || triggerType;
  };

  return (
    <div className="space-y-6">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div>
          <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
            {__("Email Sequences")}
          </h2>
          <p className="text-sm text-gray-500 dark:text-gray-400">
            {__("Create automated email workflows triggered by events")}
          </p>
        </div>
        <Button
          onClick={navigateToCreate}
          className="bg-blue-600 hover:bg-blue-700 text-white"
        >
          <Plus className="w-4 h-4 mr-2" />
          {__("Create Sequence")}
        </Button>
      </div>

      {/* Sequences List */}
      {sequences.length === 0 ? (
        <Card>
          <CardContent className="p-8 text-center">
            <GitBranch className="w-12 h-12 text-gray-300 dark:text-gray-600 mx-auto mb-4" />
            <h3 className="text-lg font-medium text-gray-900 dark:text-white mb-2">
              {__("No Sequences Yet")}
            </h3>
            <p className="text-gray-500 dark:text-gray-400 mb-4">
              {__(
                "Create your first automated email sequence to engage customers at the right time.",
              )}
            </p>
            <Button
              onClick={navigateToCreate}
              className="bg-blue-600 hover:bg-blue-700 text-white"
            >
              <Plus className="w-4 h-4 mr-2" />
              {__("Create Your First Sequence")}
            </Button>
          </CardContent>
        </Card>
      ) : (
        <div className="space-y-4">
          {sequences.map((sequence) => (
            <Card key={sequence.id}>
              <CardContent className="p-4">
                <div className="flex items-center justify-between">
                  <div className="flex-1">
                    <div className="flex items-center gap-3">
                      <h3 className="font-medium text-gray-900 dark:text-white">
                        {sequence.name}
                      </h3>
                      {getStatusBadge(sequence.status)}
                    </div>
                    <p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
                      {sequence.description || __("No description")}
                    </p>
                    <div className="flex items-center gap-4 mt-2 text-xs text-gray-500 dark:text-gray-400">
                      <span className="flex items-center gap-1">
                        <Zap className="w-3 h-3" />
                        {getTriggerLabel(sequence.trigger_type)}
                      </span>
                      <span className="flex items-center gap-1">
                        <GitBranch className="w-3 h-3" />
                        {sequence.steps_count || 0} {__("steps")}
                      </span>
                    </div>
                  </div>

                  <div className="flex items-center gap-2">
                    {sequence.status === "active" ? (
                      <Button
                        variant="ghost"
                        size="sm"
                        onClick={() =>
                          toggleStatusMutation.mutate({
                            id: sequence.id,
                            status: "paused",
                          })
                        }
                      >
                        <Pause className="w-4 h-4 text-yellow-500" />
                      </Button>
                    ) : (
                      <Button
                        variant="ghost"
                        size="sm"
                        onClick={() =>
                          toggleStatusMutation.mutate({
                            id: sequence.id,
                            status: "active",
                          })
                        }
                      >
                        <Play className="w-4 h-4 text-green-500" />
                      </Button>
                    )}
                    <Button
                      variant="outline"
                      size="sm"
                      onClick={() => navigateToEdit(sequence.id)}
                    >
                      <Edit className="w-4 h-4 mr-1" />
                      {__("Edit")}
                    </Button>
                    <Button
                      variant="ghost"
                      size="sm"
                      onClick={() => {
                        if (
                          confirm(
                            __(
                              "Are you sure you want to delete this sequence?",
                            ),
                          )
                        ) {
                          deleteMutation.mutate(sequence.id);
                        }
                      }}
                    >
                      <Trash2 className="w-4 h-4 text-red-500" />
                    </Button>
                  </div>
                </div>
              </CardContent>
            </Card>
          ))}
        </div>
      )}
    </div>
  );
};

const EmailLogsList: React.FC = () => {
  const [page, setPage] = useState(1);
  const perPage = 20;
  // Modal state for the per-row "View details" action. Holds the
  // selected log row so the modal can render the full payload without
  // a second API roundtrip — the list query already returns body +
  // metadata.
  const [viewingLog, setViewingLog] = useState<EmailLog | null>(null);

  const { data: logsData, isLoading } = useQuery({
    queryKey: ["email-logs", page, perPage],
    queryFn: () => fetchEmailLogs({ page, per_page: perPage }),
    enabled: isEmailAutomationModuleEnabled(),
  });

  const logs = (logsData?.items ?? []) as EmailLog[];
  const totalItems = logsData?.total ?? 0;
  const totalPages = Math.max(1, Math.ceil(totalItems / perPage));

  if (isLoading) {
    return (
      <Card>
        <CardHeader>
          <div className="h-6 w-28 bg-gray-200 dark:bg-gray-700 rounded animate-pulse"></div>
        </CardHeader>
        <CardContent className="p-0">
          <div className="overflow-x-auto">
            <table className="w-full">
              <thead className="bg-gray-50 dark:bg-gray-800">
                <tr>
                  {[1, 2, 3, 4, 5].map((i) => (
                    <th key={i} className="px-4 py-3">
                      <div className="h-4 w-20 bg-gray-200 dark:bg-gray-700 rounded animate-pulse"></div>
                    </th>
                  ))}
                </tr>
              </thead>
              <tbody className="divide-y divide-gray-200 dark:divide-gray-700">
                {[1, 2, 3, 4, 5].map((row) => (
                  <tr key={row}>
                    {[1, 2, 3, 4, 5].map((col) => (
                      <td key={col} className="px-4 py-3">
                        <div className="h-4 w-24 bg-gray-200 dark:bg-gray-700 rounded animate-pulse"></div>
                      </td>
                    ))}
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </CardContent>
      </Card>
    );
  }

  return (
    <Card>
      <CardHeader>
        <CardTitle className="flex items-center gap-2">
          <Clock className="w-5 h-5 text-blue-500" />
          {__("Email Logs")}
        </CardTitle>
      </CardHeader>
      <CardContent className="p-0">
        {logs.length === 0 ? (
          <div className="p-8 text-center text-gray-500 dark:text-gray-400">
            {__("No emails sent yet")}
          </div>
        ) : (
          <div className="overflow-x-auto">
            <table className="w-full">
              <thead className="bg-gray-50 dark:bg-gray-800">
                <tr>
                  <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">
                    {__("Recipient")}
                  </th>
                  <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">
                    {__("Subject")}
                  </th>
                  <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">
                    {__("Template")}
                  </th>
                  <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">
                    {__("Status")}
                  </th>
                  <th className="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">
                    {__("Sent")}
                  </th>
                  <th className="px-4 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">
                    {__("Actions")}
                  </th>
                </tr>
              </thead>
              <tbody className="divide-y divide-gray-200 dark:divide-gray-700">
                {logs.map((log) => (
                  <tr
                    key={log.id}
                    className="hover:bg-gray-50 dark:hover:bg-gray-800/50"
                  >
                    <td className="px-4 py-3">
                      <div className="text-sm font-medium text-gray-900 dark:text-white">
                        {log.recipient_name || "-"}
                      </div>
                      <div className="text-sm text-gray-500 dark:text-gray-400">
                        {log.recipient_email}
                      </div>
                    </td>
                    <td className="px-4 py-3 text-sm text-gray-900 dark:text-white max-w-xs truncate">
                      {log.subject}
                    </td>
                    <td className="px-4 py-3 text-sm text-gray-500 dark:text-gray-400">
                      {log.template_key}
                    </td>
                    <td className="px-4 py-3">
                      <Badge
                        className={
                          log.status === "sent"
                            ? "bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400"
                            : log.status === "failed"
                              ? "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400"
                              : "bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400"
                        }
                      >
                        {log.status}
                      </Badge>
                    </td>
                    <td className="px-4 py-3 text-sm text-gray-500 dark:text-gray-400">
                      {log.sent_at
                        ? new Date(log.sent_at).toLocaleString()
                        : "—"}
                    </td>
                    <td className="px-4 py-3 text-right">
                      <button
                        type="button"
                        onClick={() => setViewingLog(log)}
                        className="text-xs font-medium text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300 hover:underline"
                      >
                        {__("View details", "yatra")}
                      </button>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        )}
        {logs.length > 0 && totalPages > 1 && (
          <div className="border-t border-gray-200 px-4 py-3 dark:border-gray-700">
            <Pagination
              currentPage={page}
              totalPages={totalPages}
              totalItems={totalItems}
              itemsPerPage={perPage}
              onPageChange={setPage}
              itemName={__("entries", "yatra")}
            />
          </div>
        )}
      </CardContent>

      {viewingLog && (
        <EmailLogDetailsModal
          log={viewingLog}
          onClose={() => setViewingLog(null)}
        />
      )}
    </Card>
  );
};

/* -------------------------------------------------------------------------- */
/*  EmailLogDetailsModal                                                      */
/*                                                                            */
/*  Read-only inspector for a single email log row. Shows recipient, status, */
/*  template, full HTML body, error message (if failed), and metadata JSON.  */
/*                                                                            */
/*  Body is rendered inside a sandboxed iframe (srcdoc + sandbox="") so:      */
/*    1. Operator-visible markup looks like the real email                    */
/*    2. The iframe can't navigate the parent, run scripts, or read storage  */
/*    3. Plugin CSS doesn't leak into the preview (and vice versa)           */
/* -------------------------------------------------------------------------- */

const EmailLogDetailsModal: React.FC<{
  log: EmailLog;
  onClose: () => void;
}> = ({ log, onClose }) => {
  // Body view toggle — "preview" renders the HTML in a sandboxed
  // iframe; "raw" shows the source markup in a <pre>. Operators
  // debugging template variables / merge tags want the raw view to
  // verify substitutions; visual proofing wants the preview.
  const [bodyView, setBodyView] = useState<"preview" | "raw">("preview");
  // Tracks whether the raw HTML has been copied to clipboard so we
  // can flash a "Copied!" label briefly.
  const [copied, setCopied] = useState(false);
  const copyRawToClipboard = async () => {
    if (!log.body) return;
    try {
      await navigator.clipboard.writeText(log.body);
      setCopied(true);
      window.setTimeout(() => setCopied(false), 1500);
    } catch {
      /* clipboard denied — silently skip */
    }
  };

  // Parse metadata defensively — server stores it as a JSON string in
  // longtext, but some code paths may deliver it as a parsed object.
  const metadata: Record<string, unknown> | null = useMemo(() => {
    if (!log.metadata) return null;
    if (typeof log.metadata === "object")
      return log.metadata as Record<string, unknown>;
    try {
      const parsed = JSON.parse(String(log.metadata));
      return typeof parsed === "object" && parsed !== null ? parsed : null;
    } catch {
      return null;
    }
  }, [log.metadata]);

  const statusClass =
    log.status === "sent"
      ? "bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400"
      : log.status === "failed"
        ? "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400"
        : "bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400";

  return (
    <Modal
      isOpen
      onClose={onClose}
      title={
        <div className="flex items-center gap-2">
          <Mail className="w-5 h-5 text-blue-500" />
          {__("Email details", "yatra")}
        </div>
      }
      size="full"
      footer={
        <div className="flex justify-end">
          <Button variant="outline" onClick={onClose}>
            {__("Close", "yatra")}
          </Button>
        </div>
      }
    >
      <div className="space-y-4">
        {/* Header strip — status + sent_at + recipient at a glance */}
        <div className="rounded-md border border-gray-200 dark:border-gray-700 p-3 space-y-2">
          <div className="flex flex-wrap items-center gap-2">
            <Badge className={statusClass}>{log.status}</Badge>
            <span className="text-xs text-gray-500 dark:text-gray-400">
              {log.sent_at ? new Date(log.sent_at).toLocaleString() : "—"}
            </span>
          </div>
          <dl className="grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-2 text-sm">
            <DetailRow label={__("Recipient", "yatra")}>
              <div className="font-medium text-gray-900 dark:text-white">
                {log.recipient_name || "—"}
              </div>
              <div className="text-gray-500 dark:text-gray-400 break-all">
                {log.recipient_email}
              </div>
            </DetailRow>
            <DetailRow label={__("Subject", "yatra")}>
              <span className="text-gray-900 dark:text-white break-words">
                {log.subject || "—"}
              </span>
            </DetailRow>
            <DetailRow label={__("Template", "yatra")}>
              <code className="text-xs bg-gray-100 dark:bg-gray-800 px-1.5 py-0.5 rounded">
                {log.template_key || "—"}
              </code>
              {log.template_id ? (
                <span className="text-xs text-gray-500 ml-1">
                  (id {log.template_id})
                </span>
              ) : null}
            </DetailRow>
            <DetailRow label={__("Sequence", "yatra")}>
              {log.sequence_id ? (
                <span className="text-gray-900 dark:text-white">
                  #{log.sequence_id}
                </span>
              ) : (
                <span className="text-xs text-gray-400">
                  {__("Transactional (no sequence)", "yatra")}
                </span>
              )}
            </DetailRow>
            {(log.context_type || log.context_id) && (
              <DetailRow label={__("Context", "yatra")}>
                <span className="text-gray-900 dark:text-white">
                  {log.context_type || "—"}
                  {log.context_id ? ` #${log.context_id}` : ""}
                </span>
              </DetailRow>
            )}
          </dl>
        </div>

        {/* Error block — only shown on failed status */}
        {log.status === "failed" && log.error_message && (
          <Alert variant="error" title={__("Send failed", "yatra")}>
            <pre className="text-xs whitespace-pre-wrap break-words font-mono">
              {log.error_message}
            </pre>
          </Alert>
        )}

        {/* Body section. Two view modes:                                    */}
        {/*   - Preview: sandboxed iframe so the email's own CSS / inline     */}
        {/*     styles render accurately without leaking into admin chrome.   */}
        {/*     `sandbox=""` (empty string) is the strictest mode — no       */}
        {/*     scripts, no forms, no top-nav, no clickable links.            */}
        {/*   - Raw: HTML source in a <pre> so operators can verify merge    */}
        {/*     tags, debug template variable substitution, copy for tickets. */}
        <div>
          <div className="flex flex-wrap items-center justify-between gap-2 mb-1.5">
            <Label className="text-sm font-medium">
              {__("Message body", "yatra")}
            </Label>
            <div className="flex items-center gap-2">
              {/* View-toggle pills. Single-purpose styling so the active   */}
              {/* mode is unmistakable.                                    */}
              <div
                role="tablist"
                aria-label={__("Body view", "yatra")}
                className="inline-flex rounded-md border border-gray-200 dark:border-gray-700 overflow-hidden text-xs"
              >
                <button
                  type="button"
                  role="tab"
                  aria-selected={bodyView === "preview"}
                  onClick={() => setBodyView("preview")}
                  className={`px-3 py-1 font-medium transition-colors ${
                    bodyView === "preview"
                      ? "bg-blue-600 text-white"
                      : "bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"
                  }`}
                >
                  {__("Preview", "yatra")}
                </button>
                <button
                  type="button"
                  role="tab"
                  aria-selected={bodyView === "raw"}
                  onClick={() => setBodyView("raw")}
                  className={`px-3 py-1 font-medium border-l border-gray-200 dark:border-gray-700 transition-colors ${
                    bodyView === "raw"
                      ? "bg-blue-600 text-white"
                      : "bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"
                  }`}
                >
                  {__("Raw HTML", "yatra")}
                </button>
              </div>
              {bodyView === "raw" && log.body && (
                <button
                  type="button"
                  onClick={copyRawToClipboard}
                  className="text-xs font-medium text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300 hover:underline"
                >
                  {copied ? __("Copied!", "yatra") : __("Copy", "yatra")}
                </button>
              )}
              {bodyView === "preview" && (
                <span className="text-xs text-gray-400 hidden sm:inline">
                  {__("Sandboxed — links not clickable", "yatra")}
                </span>
              )}
            </div>
          </div>
          {log.body && log.body.trim() !== "" ? (
            bodyView === "preview" ? (
              <iframe
                title={__("Email body preview", "yatra")}
                srcDoc={log.body}
                sandbox=""
                className="w-full h-[480px] rounded-md border border-gray-200 dark:border-gray-700 bg-white"
              />
            ) : (
              <pre className="w-full h-[480px] text-xs bg-gray-50 dark:bg-gray-900/40 border border-gray-200 dark:border-gray-700 rounded-md p-3 overflow-auto whitespace-pre-wrap break-all font-mono text-gray-800 dark:text-gray-200">
                {log.body}
              </pre>
            )
          ) : (
            <div className="text-sm text-gray-400 italic rounded-md border border-dashed border-gray-200 dark:border-gray-700 p-4 text-center">
              {__("(Body not recorded for this log entry.)", "yatra")}
            </div>
          )}
        </div>

        {/* Metadata JSON — collapsible-style block. Useful for debugging   */}
        {/* operator-side: queue id, retry count, headers, attachment names. */}
        {metadata && Object.keys(metadata).length > 0 && (
          <div>
            <Label className="text-sm font-medium block mb-1.5">
              {__("Metadata", "yatra")}
            </Label>
            <pre className="text-xs bg-gray-50 dark:bg-gray-900/40 border border-gray-200 dark:border-gray-700 rounded-md p-3 overflow-x-auto max-h-48 overflow-y-auto font-mono">
              {JSON.stringify(metadata, null, 2)}
            </pre>
          </div>
        )}
      </div>
    </Modal>
  );
};

const DetailRow: React.FC<{ label: string; children: React.ReactNode }> = ({
  label,
  children,
}) => (
  <div>
    <dt className="text-[11px] uppercase tracking-wide text-gray-500 dark:text-gray-400 font-medium">
      {label}
    </dt>
    <dd className="mt-0.5">{children}</dd>
  </div>
);

type EmailHubTab = "delivery" | "templates" | "sequences" | "logs";

function getInitialEmailHubTab(): EmailHubTab {
  if (typeof window === "undefined") return "delivery";
  const tab = new URLSearchParams(window.location.search).get("tab");
  if (tab === "sequences" || tab === "sequence") return "sequences";
  if (tab === "template" || tab === "templates") return "templates";
  if (tab === "logs") return "logs";
  if (tab === "delivery" || tab === "core") return "delivery";
  return "delivery";
}

const EmailAutomationModulePrompt: React.FC = () => (
  <div className="rounded-xl border border-amber-200 bg-amber-50 p-6 dark:border-amber-800 dark:bg-amber-950/30">
    <div className="flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between">
      <div className="flex gap-3">
        <Settings2 className="mt-0.5 h-6 w-6 shrink-0 text-amber-600 dark:text-amber-400" />
        <div>
          <h3 className="text-base font-semibold text-amber-900 dark:text-amber-100">
            {__("Enable the Email Automation module", "yatra")}
          </h3>
          <p className="mt-1 text-sm text-amber-800/90 dark:text-amber-200/90">
            {__(
              "Yatra Pro is active. Turn on Email Automation under Modules to use custom templates, sequences, and send logs.",
              "yatra",
            )}
          </p>
        </div>
      </div>
      <Button
        type="button"
        variant="outline"
        className="shrink-0 border-amber-300 bg-white hover:bg-amber-100 dark:border-amber-700 dark:bg-amber-950 dark:hover:bg-amber-900"
        onClick={() => {
          window.location.href = "admin.php?page=yatra&subpage=modules";
        }}
      >
        {__("Open Modules", "yatra")}
      </Button>
    </div>
  </div>
);

const EmailAutomation: React.FC = () => {
  const emailMgr = useEmailSettingsManager();
  const [activeTab, setActiveTab] = useState<EmailHubTab>(() => {
    const t = getInitialEmailHubTab();
    if (!isProPluginActive() && (t === "sequences" || t === "logs")) {
      return "delivery";
    }
    return t;
  });

  /**
   * Persist tab changes to the URL so the operator can:
   *   - bookmark / share a direct link to a specific tab
   *   - use the browser back button to step through tab history
   *   - reload the page and stay on the same tab
   *
   * Using `replaceState` (not `pushState`) for the initial sync so we
   * don't add a bogus history entry on first mount; subsequent user-
   * driven tab switches go through `switchTab` which uses
   * `replaceState` too — back/forward should navigate AWAY from this
   * page entirely, not cycle through its tabs.
   */
  React.useEffect(() => {
    if (typeof window === "undefined") return;
    const url = new URL(window.location.href);
    if (url.searchParams.get("tab") === activeTab) return;
    url.searchParams.set("tab", activeTab);
    window.history.replaceState({}, "", url.toString());
  }, [activeTab]);

  const switchTab = (next: EmailHubTab) => setActiveTab(next);

  const showProAutomationExtras = isProPluginActive();
  const automationReady = isEmailAutomationModuleEnabled();

  const tabs = [
    {
      key: "delivery" as const,
      label: __("Delivery", "yatra"),
      icon: Send,
    },
    {
      key: "templates" as const,
      label: __("Templates", "yatra"),
      icon: FileText,
    },
    ...(showProAutomationExtras
      ? [
          {
            key: "sequences" as const,
            label: __("Sequences", "yatra"),
            icon: GitBranch,
          },
          {
            key: "logs" as const,
            label: __("Email logs", "yatra"),
            icon: Mail,
          },
        ]
      : []),
  ];

  return (
    <div className="space-y-6">
      <PageHeader
        description={__(
          "Delivery (addresses & SMTP), core templates for everyone, and optional Pro sequences and logs.",
          "yatra",
        )}
      />

      <Card>
        <div className="border-b border-gray-200 dark:border-gray-700">
          <nav className="flex flex-wrap gap-1 px-4">
            {tabs.map((tab) => (
              <button
                key={tab.key}
                type="button"
                onClick={() => switchTab(tab.key)}
                className={`flex items-center gap-2 px-4 py-3 border-b-2 font-medium text-sm transition-colors ${
                  activeTab === tab.key
                    ? "border-blue-500 text-blue-600 dark:text-blue-400"
                    : "border-transparent text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300 hover:border-gray-300 dark:hover:border-gray-600"
                }`}
              >
                <tab.icon className="w-4 h-4" />
                {tab.label}
              </button>
            ))}
          </nav>
        </div>

        <CardContent className="p-6">
          {activeTab === "delivery" && (
            <>
              {!emailMgr.ready ? (
                <div className="flex justify-center py-16">
                  <Loader2 className="h-10 w-10 animate-spin text-gray-400" />
                </div>
              ) : (
                <EmailDeliverySection
                  values={emailMgr.values}
                  onFieldChange={emailMgr.handleFieldChange}
                />
              )}
            </>
          )}

          {activeTab === "templates" && (
            <>
              {automationReady && (
                <p className="mb-6 text-sm text-gray-600 dark:text-gray-400">
                  {__(
                    "Customer booking, payment, cancellation, and trip-reminder emails use the matching template when its body is filled; otherwise the free plugin defaults or settings HTML apply. Admin: New Booking sends when that template has a body (plain-text admin notices from checkout and notifications are skipped to avoid duplicates). Other booking.created templates send in addition to the customer email. Booking Confirmed uses the “Booking Confirmed” row when a booking moves to confirmed; Trip Completed uses its template instead of the generic completed email when the body is filled.",
                    "yatra",
                  )}
                </p>
              )}
              {!emailMgr.ready && !automationReady ? (
                <div className="flex justify-center py-16">
                  <Loader2 className="h-10 w-10 animate-spin text-gray-400" />
                </div>
              ) : (
                <EmailTemplatesList
                  automationModuleActive={automationReady}
                  settingsBridge={
                    automationReady
                      ? undefined
                      : {
                          values: emailMgr.values,
                          onFieldChange: emailMgr.handleFieldChange,
                        }
                  }
                />
              )}
            </>
          )}

          {activeTab === "sequences" &&
            (automationReady ? (
              <EmailSequencesList />
            ) : isProPluginActive() ? (
              <EmailAutomationModulePrompt />
            ) : (
              <PremiumUpgradeCard />
            ))}

          {activeTab === "logs" &&
            (automationReady ? (
              <EmailLogsList />
            ) : isProPluginActive() ? (
              <EmailAutomationModulePrompt />
            ) : (
              <PremiumUpgradeCard />
            ))}
        </CardContent>

        {(activeTab === "delivery" || activeTab === "templates") &&
          emailMgr.ready && (
            <CardFooter className="flex justify-end border-t border-gray-100 pt-4 dark:border-gray-700/50">
              <Button
                type="button"
                onClick={emailMgr.save}
                disabled={!emailMgr.canSave}
                className="flex items-center gap-2"
              >
                {emailMgr.isSaving ? (
                  <>
                    <Loader2 className="h-4 w-4 animate-spin" />
                    {__("Saving…", "yatra")}
                  </>
                ) : (
                  <>
                    <Save className="h-4 w-4" />
                    {__("Save email settings", "yatra")}
                  </>
                )}
              </Button>
            </CardFooter>
          )}
      </Card>
    </div>
  );
};

export default EmailAutomation;
