"use client";

import React, { useState, useEffect } from "react";
import { motion, useReducedMotion } from "motion/react";
import {
  Mic,
  MessageSquare,
  PhoneCall,
  Globe,
  User,
} from "lucide-react";
import Image from "next/image";
import { SpotlightCard } from "@/components/spotlight-card";

export function IndustryTechSection() {
  const reduce = useReducedMotion();

  // Typewriter effect for "Remind me to check..."
  const fullText = "Remind me to check...";
  const [displayedText, setDisplayedText] = useState("");
  const [isDeleting, setIsDeleting] = useState(false);

  useEffect(() => {
    if (reduce) {
      return;
    }
    const timer = setTimeout(
      () => {
        if (!isDeleting) {
          if (displayedText.length < fullText.length) {
            setDisplayedText(fullText.slice(0, displayedText.length + 1));
          } else {
            setTimeout(() => setIsDeleting(true), 1600);
          }
        } else {
          if (displayedText.length > 0) {
            setDisplayedText(fullText.slice(0, displayedText.length - 1));
          } else {
            setIsDeleting(false);
          }
        }
      },
      isDeleting ? 40 : 90,
    );
    return () => clearTimeout(timer);
  }, [displayedText, isDeleting, reduce]);

  return (
    <div className="grid gap-6 md:grid-cols-2">
      {/* ──────────────────────────────────────────────────────────
          CARD 1: Global Citizens
      ────────────────────────────────────────────────────────── */}
      <SpotlightCard className="flex flex-col justify-between rounded-3xl border border-border/80 bg-card p-6 md:p-8 shadow-xs hover:border-[#4FDAF1]/40 transition-colors">
        <div>
          <h3 className="font-display text-xl font-bold text-foreground">
            <span className="font-extrabold text-gradient-cool">Global</span> Citizens
          </h3>
          <p className="mt-2 text-sm text-muted-foreground leading-relaxed">
            Quto Agents inherently understand 20+ languages, with SOTA accuracy in all.
          </p>
        </div>

        {/* Animated Multilingual Word Cloud */}
        <div className="relative mt-8 flex min-h-[190px] flex-col items-center justify-center overflow-hidden rounded-2xl bg-secondary/30 p-6">
          <div className="flex flex-col items-center gap-3 select-none">
            {/* Row 1 */}
            <div className="flex items-center gap-6 text-sm font-medium text-muted-foreground/70">
              <motion.span
                animate={reduce ? undefined : { y: [0, -3, 0] }}
                transition={{ duration: 3, repeat: Infinity, ease: "easeInOut" }}
              >
                Hello
              </motion.span>
              <motion.span
                animate={reduce ? undefined : { y: [0, 3, 0] }}
                transition={{ duration: 3.4, repeat: Infinity, ease: "easeInOut", delay: 0.3 }}
              >
                Hindi
              </motion.span>
              <motion.span
                animate={reduce ? undefined : { y: [0, -2, 0] }}
                transition={{ duration: 2.8, repeat: Infinity, ease: "easeInOut", delay: 0.6 }}
              >
                Malayalam
              </motion.span>
            </div>

            {/* Row 2 (Center Hero Badge) */}
            <div className="flex items-center gap-5">
              <motion.span
                animate={reduce ? undefined : { scale: [1, 1.03, 1] }}
                transition={{ duration: 2.5, repeat: Infinity, ease: "easeInOut" }}
                className="font-display text-xl font-extrabold tracking-tight text-gradient-brand drop-shadow-xs px-3.5 py-1 rounded-full border border-[#4FDAF1]/35 bg-[#4FDAF1]/10"
              >
                20+ languages
              </motion.span>
              <span className="text-base font-medium text-muted-foreground/60">
                வணக்கம்
              </span>
              <span className="text-base font-medium text-muted-foreground/50">
                হ্যালো
              </span>
            </div>

            {/* Row 3 */}
            <div className="flex items-center gap-5 text-sm font-medium text-muted-foreground/70">
              <span>French</span>
              <span className="text-base text-muted-foreground/60">ಕನ್ನಡ</span>
              <span className="text-base font-semibold text-muted-foreground/80">नमस्कार</span>
              <span className="text-base text-muted-foreground/60">హలో</span>
            </div>

            {/* Row 4 */}
            <div className="flex items-center gap-6 text-sm font-medium text-muted-foreground/50">
              <span className="text-base">你好</span>
              <span>Hola</span>
              <span className="text-base text-muted-foreground/70 font-semibold">नमस्ते</span>
              <span>Привет</span>
            </div>
          </div>
        </div>
      </SpotlightCard>

      {/* ──────────────────────────────────────────────────────────
          CARD 2: Lowest Latency
      ────────────────────────────────────────────────────────── */}
      <SpotlightCard className="flex flex-col justify-between rounded-3xl border border-border/80 bg-card p-6 md:p-8 shadow-xs hover:border-[#4FDAF1]/40 transition-colors">
        <div>
          <h3 className="font-display text-xl font-bold text-foreground">
            <span className="font-extrabold text-gradient-brand">Lowest latency</span>
          </h3>
          <p className="mt-2 text-sm text-muted-foreground leading-relaxed">
            Quto&apos;s orchestration brings responses in less than 400 ms across modalities.
          </p>
        </div>

        {/* Animated Waveform + Dark Speech Capsule */}
        <div className="relative mt-8 flex min-h-[190px] items-center justify-center overflow-hidden rounded-2xl bg-secondary/30 p-6">
          {/* Subtle Background Audio Waveform with subtle cyan tint */}
          <div className="absolute inset-x-0 flex items-center justify-center gap-1 opacity-35">
            {Array.from({ length: 42 }).map((_, i) => {
              const baseHeight = 12 + Math.sin(i * 0.35) * 22 + ((i * 7) % 18);
              return (
                <motion.span
                  key={i}
                  className="w-1 rounded-full bg-[#4FDAF1]"
                  style={{ height: baseHeight }}
                  animate={
                    reduce
                      ? undefined
                      : {
                          scaleY: [0.4, 1, 0.4],
                        }
                  }
                  transition={{
                    duration: 1.2 + (i % 6) * 0.2,
                    repeat: Infinity,
                    ease: "easeInOut",
                    delay: (i % 10) * 0.08,
                  }}
                />
              );
            })}
          </div>

          {/* Foreground High-Contrast Audio Capsule Pill */}
          <div className="relative z-10 flex items-center gap-3 rounded-full border border-[#4FDAF1]/40 bg-foreground px-4 py-2.5 shadow-[0_0_20px_-5px_rgba(79,218,241,0.35)]">
            {/* Dotted Sound Matrix Equalizer in Bright Cyan */}
            <div className="flex items-center gap-1 px-1">
              {Array.from({ length: 6 }).map((_, col) => (
                <div key={col} className="flex flex-col gap-0.5">
                  {Array.from({ length: 4 }).map((_, row) => (
                    <motion.span
                      key={row}
                      className="size-1 rounded-full bg-[#4FDAF1]"
                      animate={
                        reduce
                          ? undefined
                          : {
                              opacity: [0.3, 1, 0.3],
                              scale: [0.8, 1.2, 0.8],
                            }
                      }
                      transition={{
                        duration: 0.6 + (col % 3) * 0.2,
                        repeat: Infinity,
                        ease: "easeInOut",
                        delay: (col + row) * 0.08,
                      }}
                    />
                  ))}
                </div>
              ))}
            </div>

            {/* Live Typing Speech Text */}
            <div className="flex items-center font-medium text-xs text-background">
              <span>{reduce ? fullText : displayedText}</span>
              <motion.span
                animate={{ opacity: [1, 0, 1] }}
                transition={{ duration: 0.8, repeat: Infinity }}
                className="ml-0.5 inline-block w-1.5 h-3.5 bg-[#4FDAF1]"
              />
            </div>
          </div>
        </div>
      </SpotlightCard>

      {/* ──────────────────────────────────────────────────────────
          CARD 3: Maximum Scale
      ────────────────────────────────────────────────────────── */}
      <SpotlightCard className="flex flex-col justify-between rounded-3xl border border-border/80 bg-card p-6 md:p-8 shadow-xs hover:border-[#E26B0B]/40 transition-colors">
        <div>
          <h3 className="font-display text-xl font-bold text-foreground">
            <span className="font-extrabold text-gradient-warm">Maximum scale</span>
          </h3>
          <p className="mt-2 text-sm text-muted-foreground leading-relaxed">
            Always prepared for surges, with no drop in performance.
          </p>
        </div>

        {/* 3D Perspective Floor Grid with Laser Streaks & Floating 120K+ Card */}
        <div className="relative mt-8 flex min-h-[190px] items-center justify-center overflow-hidden rounded-2xl bg-secondary/30 p-6">
          {/* 3D Perspective Lines Floor/Ceiling */}
          <div className="absolute inset-0 overflow-hidden [mask-image:radial-gradient(ellipse_70%_60%_at_50%_50%,#000_40%,transparent_90%)]">
            <svg
              className="absolute inset-0 size-full stroke-foreground/15"
              width="100%"
              height="100%"
            >
              {/* Floor Perspective Rays converging to center */}
              <line x1="0%" y1="100%" x2="50%" y2="50%" strokeWidth="1" />
              <line x1="20%" y1="100%" x2="50%" y2="50%" strokeWidth="1" />
              <line x1="40%" y1="100%" x2="50%" y2="50%" strokeWidth="1" />
              <line x1="60%" y1="100%" x2="50%" y2="50%" strokeWidth="1" />
              <line x1="80%" y1="100%" x2="50%" y2="50%" strokeWidth="1" />
              <line x1="100%" y1="100%" x2="50%" y2="50%" strokeWidth="1" />

              {/* Ceiling Perspective Rays */}
              <line x1="0%" y1="0%" x2="50%" y2="50%" strokeWidth="1" />
              <line x1="20%" y1="0%" x2="50%" y2="50%" strokeWidth="1" />
              <line x1="80%" y1="0%" x2="50%" y2="50%" strokeWidth="1" />
              <line x1="100%" y1="0%" x2="50%" y2="50%" strokeWidth="1" />

              {/* Grid Horizontal Depth Rings */}
              <line x1="10%" y1="90%" x2="90%" y2="90%" strokeWidth="1" />
              <line x1="20%" y1="80%" x2="80%" y2="80%" strokeWidth="1" />
              <line x1="30%" y1="70%" x2="70%" y2="70%" strokeWidth="1" />
              <line x1="38%" y1="60%" x2="62%" y2="60%" strokeWidth="1" />
            </svg>

            {/* Glowing Animated Laser Beams Shooting in Perspective */}
            <motion.div
              className="absolute top-1/4 left-1/4 h-2 w-16 -rotate-45 rounded-full bg-gradient-to-r from-transparent via-[#4FDAF1]/80 to-transparent blur-[1px]"
              animate={
                reduce
                  ? undefined
                  : {
                      x: [-40, 180],
                      y: [-20, 100],
                      opacity: [0, 1, 0],
                    }
              }
              transition={{ duration: 2.2, repeat: Infinity, ease: "easeInOut" }}
            />
            <motion.div
              className="absolute bottom-1/4 right-1/4 h-2 w-20 rotate-45 rounded-full bg-gradient-to-r from-transparent via-[#E26B0B]/80 to-transparent blur-[1px]"
              animate={
                reduce
                  ? undefined
                  : {
                      x: [40, -180],
                      y: [20, -100],
                      opacity: [0, 1, 0],
                    }
              }
              transition={{ duration: 2.8, repeat: Infinity, ease: "easeInOut", delay: 0.8 }}
            />
            <motion.div
              className="absolute bottom-4 left-6 h-2.5 w-14 rotate-12 rounded-full bg-gradient-to-r from-transparent via-[#67B7E7]/80 to-transparent blur-[0.5px]"
              animate={
                reduce
                  ? undefined
                  : {
                      scale: [0.9, 1.1, 0.9],
                      opacity: [0.4, 0.9, 0.4],
                    }
              }
              transition={{ duration: 1.8, repeat: Infinity, ease: "easeInOut" }}
            />
          </div>

          {/* Center Floating Scale Metrics Card */}
          <div className="relative z-10 flex flex-col items-center justify-center rounded-xl border border-[#E26B0B]/40 bg-background/95 px-8 py-5 shadow-[0_0_30px_-5px_rgba(226,107,11,0.25)] backdrop-blur-md">
            <span className="font-display text-4xl font-extrabold tracking-tight text-gradient-brand md:text-5xl">
              120K+
            </span>
            <span className="mt-1 text-[10px] font-bold uppercase tracking-widest text-[#E26B0B]">
              Interactions / Hour
            </span>
          </div>
        </div>
      </SpotlightCard>

      {/* ──────────────────────────────────────────────────────────
          CARD 4: Unified Context
      ────────────────────────────────────────────────────────── */}
      <SpotlightCard className="flex flex-col justify-between rounded-3xl border border-border/80 bg-card p-6 md:p-8 shadow-xs hover:border-[#4FDAF1]/40 transition-colors">
        <div>
          <h3 className="font-display text-xl font-bold text-foreground">
            <span className="font-extrabold text-gradient-brand">Unified Context</span>
          </h3>
          <p className="mt-2 text-sm text-muted-foreground leading-relaxed">
            Agents that remember all your conversations across channels.
          </p>
        </div>

        {/* Animated Connected Node Network (Matching Screenshot 4) */}
        <div className="relative mt-8 flex min-h-[190px] items-center justify-center overflow-hidden rounded-2xl bg-secondary/30 p-6">
          <div className="relative flex w-full max-w-sm items-center justify-between">
            {/* Node 1: User Profile */}
            <div className="relative z-10 flex size-9 items-center justify-center rounded-full border border-border bg-card shadow-sm">
              <User className="size-4 text-muted-foreground" />
            </div>

            {/* Connecting SVG with Traveling Energy Pulses */}
            <svg
              viewBox="0 0 100 100"
              preserveAspectRatio="none"
              className="absolute inset-0 size-full pointer-events-none"
              style={{ overflow: "visible" }}
            >
              {/* User → Center connector */}
              <line
                x1="6"
                y1="50"
                x2="50"
                y2="50"
                stroke="currentColor"
                strokeWidth="1.5"
                vectorEffect="non-scaling-stroke"
                className="text-border/80"
              />

              {/* Curved connectors, Center → 4 channels */}
              <path d="M 50 50 C 70 50, 75 12, 92 12" fill="none" stroke="currentColor" strokeWidth="1.5" vectorEffect="non-scaling-stroke" className="text-border/80" />
              <path d="M 50 50 C 70 50, 75 37, 92 37" fill="none" stroke="currentColor" strokeWidth="1.5" vectorEffect="non-scaling-stroke" className="text-border/80" />
              <path d="M 50 50 C 70 50, 75 63, 92 63" fill="none" stroke="currentColor" strokeWidth="1.5" vectorEffect="non-scaling-stroke" className="text-border/80" />
              <path d="M 50 50 C 70 50, 75 88, 92 88" fill="none" stroke="currentColor" strokeWidth="1.5" vectorEffect="non-scaling-stroke" className="text-border/80" />

              {/* Pulsing nodes with reference palette accents */}
              <circle cx={50} cy={50} r="1.5" className="fill-[#4FDAF1] opacity-90 motion-safe:animate-pulse motion-reduce:opacity-60" style={{ animationDuration: "1.8s" }} />
              <circle cx={92} cy={12} r="1.2" className="fill-[#67B7E7] opacity-80 motion-safe:animate-pulse motion-reduce:opacity-60" style={{ animationDuration: "1.6s", animationDelay: "0.4s" }} />
              <circle cx={92} cy={37} r="1.2" className="fill-[#4FDAF1] opacity-80 motion-safe:animate-pulse motion-reduce:opacity-60" style={{ animationDuration: "1.6s", animationDelay: "0.6s" }} />
              <circle cx={92} cy={63} r="1.2" className="fill-[#E26B0B] opacity-80 motion-safe:animate-pulse motion-reduce:opacity-60" style={{ animationDuration: "1.6s", animationDelay: "0.8s" }} />
              <circle cx={92} cy={88} r="1.2" className="fill-[#F79C09] opacity-80 motion-safe:animate-pulse motion-reduce:opacity-60" style={{ animationDuration: "1.6s", animationDelay: "1s" }} />
            </svg>

            {/* Node 2: Center Quto Unified Brain with subtle glow halo */}
            <div className="relative z-10 flex size-12 items-center justify-center rounded-full border border-[#4FDAF1]/30 bg-[#4FDAF1]/10 p-1 shadow-[0_0_20px_rgba(79,218,241,0.25)]">
              <Image
                src="/images/quto-logo.webp"
                alt="Quto AI Core"
                width={48}
                height={48}
                className="size-10 object-contain select-none"
              />
            </div>

            {/* Node 3: Channel Endpoints Column with sequenced reference colors */}
            <div className="relative z-10 flex flex-col gap-2.5">
              <div className="flex size-7 items-center justify-center rounded-full border border-[#4FDAF1]/40 bg-[#4FDAF1]/10 shadow-xs" title="Voice / Mic">
                <Mic className="size-3.5 text-[#4FDAF1]" />
              </div>
              <div className="flex size-7 items-center justify-center rounded-full border border-[#67B7E7]/40 bg-[#67B7E7]/10 shadow-xs" title="Chat / Messages">
                <MessageSquare className="size-3.5 text-[#67B7E7]" />
              </div>
              <div className="flex size-7 items-center justify-center rounded-full border border-[#F79C09]/40 bg-[#F79C09]/10 shadow-xs" title="Phone Calls">
                <PhoneCall className="size-3.5 text-[#F79C09]" />
              </div>
              <div className="flex size-7 items-center justify-center rounded-full border border-[#E26B0B]/40 bg-[#E26B0B]/10 shadow-xs" title="Web / Global">
                <Globe className="size-3.5 text-[#E26B0B]" />
              </div>
            </div>
          </div>
        </div>
      </SpotlightCard>
    </div>
  );
}
