"use client";

import * as React from "react";
import { motion, useReducedMotion, type Variants } from "framer-motion";

export type RealTimeAgentActivityDashboardProps = Omit<
  React.ComponentProps<typeof motion.svg>,
  "children"
>;

// Original SVG geometry, unmodified.
const activityPath =
  "M95.333 52.001H84.587a8.666 8.666 0 0 0-8.364 6.327L66.04 94.555a1.083 1.083 0 0 1-2.08 0L40.04 9.448a1.083 1.083 0 0 0-2.08 0L27.776 45.675A8.67 8.67 0 0 1 19.457 52H8.667";

const activityVariants: Variants = {
  hidden: { opacity: 0, pathLength: 0 },
  visible: {
    opacity: 1,
    pathLength: 1,
    strokeWidth: 7,
    transition: {
      opacity: { duration: 0.2 },
      pathLength: { duration: 1.15, ease: "easeInOut" },
      strokeWidth: { duration: 0.2 },
    },
  },
  hover: {
    opacity: [1, 0.8, 1],
    pathLength: 1,
    strokeWidth: [7, 7.6, 7],
    transition: {
      duration: 1.9,
      ease: "easeInOut",
      repeat: Infinity,
      repeatType: "loop",
    },
  },
};

const signalVariants: Variants = {
  hidden: { opacity: 0, strokeDashoffset: 100 },
  visible: {
    opacity: 0,
    strokeDashoffset: 100,
    transition: { opacity: { duration: 0.2 } },
  },
  hover: {
    opacity: 0.95,
    strokeDashoffset: [100, 0],
    transition: {
      opacity: { duration: 0.2 },
      strokeDashoffset: {
        duration: 1.9,
        ease: "linear",
        repeat: Infinity,
        repeatType: "loop",
      },
    },
  },
};

/**
 * Draws once on mount, then displays a continuously traveling activity pulse
 * only while hovered. Leaving hover restores the original, static SVG.
 */
export default function RealTimeAgentActivityDashboard({
  width = 104,
  height = 104,
  style,
  animate,
  whileHover,
  ...props
}: RealTimeAgentActivityDashboardProps) {
  const reducedMotion = useReducedMotion();

  return (
    <motion.svg
      {...props}
      width={width}
      height={height}
      viewBox="0 0 104 104"
      fill="none"
      xmlns="http://www.w3.org/2000/svg"
      role="img"
      aria-label="Real-time agent activity dashboard"
      initial={reducedMotion ? false : "hidden"}
      animate={animate}
      whileInView={
        animate !== undefined || reducedMotion ? undefined : "visible"
      }
      viewport={{ once: true, amount: 0.55 }}
      whileHover={
        whileHover !== undefined
          ? whileHover
          : reducedMotion
            ? undefined
            : "hover"
      }
      style={{ display: "inline-block", overflow: "visible", ...style }}
    >
      {/* Hover detection works anywhere inside the icon's full square. */}
      <rect width="104" height="104" fill="transparent" />

      {/* Main heartbeat line: exact supplied path and original yellow. */}
      <motion.path
        d={activityPath}
        stroke="#ffcc4b"
        strokeWidth={7}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={activityVariants}
      />

      {/* An illuminated signal travels along the existing path on hover. */}
      {!reducedMotion && (
        <motion.path
          d={activityPath}
          fill="none"
          stroke="#fff4c6"
          strokeWidth={3.25}
          strokeLinecap="round"
          strokeLinejoin="round"
          pathLength={100}
          strokeDasharray="12 88"
          pointerEvents="none"
          variants={signalVariants}
        />
      )}
    </motion.svg>
  );
}
