"use client"

import { useEffect, useState } from "react"
import { Check, Download, X, ExternalLink } from "lucide-react"
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogDescription,
  DialogFooter,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { ContactForm } from "@/components/contact-form"

export default function PatentSummary() {
  const [isOpen, setIsOpen] = useState(false)
  const [showContactForm, setShowContactForm] = useState(false)

  useEffect(() => {
    const handleOpenPatentSummary = () => setIsOpen(true)
    window.addEventListener("openPatentSummary", handleOpenPatentSummary)

    return () => {
      window.removeEventListener("openPatentSummary", handleOpenPatentSummary)
    }
  }, [])

  const handleClose = () => {
    setIsOpen(false)
    setShowContactForm(false)
  }

  const handleDownload = () => {
    // Download the white paper
    window.open("/whitepaper", "_blank")

    // Fire analytics event
    if (typeof window !== "undefined" && window.gtag) {
      window.gtag("event", "whitepaperDownload", {
        event_category: "downloads",
        event_label: "patent_summary_modal",
      })
    }
  }

  const handleContactCTO = () => {
    // Show contact form instead of opening email client
    setShowContactForm(true)
  }

  return (
    <Dialog open={isOpen} onOpenChange={setIsOpen}>
      <DialogContent className="max-h-[90vh] max-w-4xl overflow-y-auto">
        {!showContactForm ? (
          <>
            <DialogHeader>
              <DialogTitle className="text-2xl">
                QuantumVerify Patent 1 – Quantum-secure real-time recipient verification for cross-border payments
              </DialogTitle>
              <DialogDescription>
                Filed: UKIPO, April 2025 | Status: Patent-pending (company-assigned)
              </DialogDescription>
            </DialogHeader>

            <div className="grid gap-6 md:grid-cols-5">
              <div className="md:col-span-3 space-y-4">
                <div>
                  <h3 className="text-lg font-semibold">The problem</h3>
                  <p className="text-muted-foreground">
                    Traditional correspondent banking cannot guarantee—before money leaves a sender's account—that the
                    beneficiary account exists, is usable and is unsanctioned. Multi-hop routes, legacy cryptography and
                    slow, centralised screening add 300-500 ms latency, expose PII, and leave payment providers
                    scrambling to claw funds back when something goes wrong.
                  </p>
                </div>

                <div>
                  <h3 className="text-lg font-semibold">The invention</h3>
                  <p className="text-muted-foreground">
                    Patent 1 discloses a single-hop, edge-executed protocol in which the sender's system packages a
                    data-minimised request, signs it inside a FIPS 140-2 L3 Hardware Security Module using a
                    post-quantum ML-DSA-65 signature, and sends it directly to the beneficiary bank across a
                    permissioned network. The receiving bank verifies the signature in its own HSM, runs a two-stage
                    sanctions screen (Jaro-Winkler → fine-tuned transformer) entirely inside its perimeter, and returns
                    a signed verdict—all disciplined to ± 500 ns of UTC by a hardened PTP profile.
                  </p>
                </div>

                <div>
                  <h3 className="text-lg font-semibold">Measured results (100 k-sample benchmarks):</h3>
                  <div className="rounded-md border">
                    <Table>
                      <TableHeader>
                        <TableRow>
                          <TableHead>Metric</TableHead>
                          <TableHead>Target</TableHead>
                          <TableHead>Achieved</TableHead>
                          <TableHead>Head-room</TableHead>
                        </TableRow>
                      </TableHeader>
                      <TableBody>
                        <TableRow>
                          <TableCell>End-to-end latency p50/p95</TableCell>
                          <TableCell>≤ 45 ms / 75 ms</TableCell>
                          <TableCell className="font-medium">38 ms / 69 ms</TableCell>
                          <TableCell>15% / 8%</TableCell>
                        </TableRow>
                        <TableRow>
                          <TableCell>PQC sign (median)</TableCell>
                          <TableCell>≤ 2.2 ms</TableCell>
                          <TableCell className="font-medium">1.78 ms</TableCell>
                          <TableCell>19%</TableCell>
                        </TableRow>
                        <TableRow>
                          <TableCell>Clock-skew residual (p95)</TableCell>
                          <TableCell>≤ 1 µs</TableCell>
                          <TableCell className="font-medium">0.68 µs</TableCell>
                          <TableCell>32%</TableCell>
                        </TableRow>
                        <TableRow>
                          <TableCell>Sanctions-screen recall</TableCell>
                          <TableCell>≥ 97%</TableCell>
                          <TableCell className="font-medium">97.12%</TableCell>
                          <TableCell>0.12 pp</TableCell>
                        </TableRow>
                      </TableBody>
                    </Table>
                  </div>
                </div>

                <div>
                  <h3 className="text-lg font-semibold">Why it matters</h3>
                  <ul className="space-y-2">
                    <li className="flex items-start">
                      <Check className="mr-2 h-5 w-5 text-primary flex-shrink-0 mt-0.5" />
                      <span>
                        <strong>Quantum-ready integrity</strong> – every message is future-proof against Shor-class
                        attacks.
                      </span>
                    </li>
                    <li className="flex items-start">
                      <Check className="mr-2 h-5 w-5 text-primary flex-shrink-0 mt-0.5" />
                      <span>
                        <strong>Instant-payment compatible</strong> – sub-100 ms round-trip on commodity hardware.
                      </span>
                    </li>
                    <li className="flex items-start">
                      <Check className="mr-2 h-5 w-5 text-primary flex-shrink-0 mt-0.5" />
                      <span>
                        <strong>Privacy by design</strong> – originator PII never leaves the sender; optional fields are
                        hashable.
                      </span>
                    </li>
                    <li className="flex items-start">
                      <Check className="mr-2 h-5 w-5 text-primary flex-shrink-0 mt-0.5" />
                      <span>
                        <strong>Explainable AI</strong> – each sanctions hit carries token-level heat-maps regulators
                        can audit.
                      </span>
                    </li>
                    <li className="flex items-start">
                      <Check className="mr-2 h-5 w-5 text-primary flex-shrink-0 mt-0.5" />
                      <span>
                        <strong>Drop-in adoption</strong> – no changes to existing ISO 20022 payment messages.
                      </span>
                    </li>
                  </ul>
                </div>
              </div>

              <div className="md:col-span-2">
                <div className="rounded-lg border bg-card p-4 shadow-sm">
                  <h3 className="text-lg font-semibold mb-2">Patent coverage</h3>
                  <p className="text-sm text-muted-foreground">
                    The claims cover the distributed topology, the edge-executed two-stage sanctions algorithm, the PTP
                    profile for financial services, the PQC-in-HSM signing workflow and the embedded explainability
                    objects. Competitors would need to re-engineer multiple pillars simultaneously to avoid
                    infringement.
                  </p>

                  <div className="mt-4 rounded-md bg-muted p-4">
                    <h4 className="text-sm font-medium mb-2">Key Components</h4>
                    <div className="space-y-2 text-sm">
                      <div className="flex items-start">
                        <div className="h-6 w-6 rounded-full bg-primary/20 flex items-center justify-center mr-2 flex-shrink-0">
                          <span className="text-xs font-bold text-primary">1</span>
                        </div>
                        <span>PQC Signatures in HSM</span>
                      </div>
                      <div className="flex items-start">
                        <div className="h-6 w-6 rounded-full bg-primary/20 flex items-center justify-center mr-2 flex-shrink-0">
                          <span className="text-xs font-bold text-primary">2</span>
                        </div>
                        <span>Two-stage Sanctions AI</span>
                      </div>
                      <div className="flex items-start">
                        <div className="h-6 w-6 rounded-full bg-primary/20 flex items-center justify-center mr-2 flex-shrink-0">
                          <span className="text-xs font-bold text-primary">3</span>
                        </div>
                        <span>Precision Time Protocol</span>
                      </div>
                    </div>
                  </div>

                  <p className="mt-4 text-sm text-muted-foreground">
                    For deeper technical detail see our 35-page validation white-paper or the full UKIPO filing.
                  </p>
                </div>
              </div>
            </div>

            <DialogFooter className="sticky bottom-0 bg-background pt-2">
              <div className="flex w-full flex-col-reverse sm:flex-row sm:justify-between sm:space-x-2">
                <Button variant="outline" onClick={handleClose}>
                  <X className="mr-2 h-4 w-4" />
                  Close
                </Button>
                <div className="flex flex-col space-y-2 sm:flex-row sm:space-x-2 sm:space-y-0">
                  <Button variant="outline" onClick={handleContactCTO}>
                    <ExternalLink className="mr-2 h-4 w-4" />
                    Contact our CTO
                  </Button>
                  <Button onClick={handleDownload}>
                    <Download className="mr-2 h-4 w-4" />
                    Download full white-paper (PDF, 3 MB)
                  </Button>
                </div>
              </div>
            </DialogFooter>
          </>
        ) : (
          <ContactForm onClose={handleClose} />
        )}
      </DialogContent>
    </Dialog>
  )
}
