"use client";

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

export type ChatHistoryProps = React.ComponentProps<typeof motion.svg>;

const outlineVariants: Variants = {
  hidden: { opacity: 0, pathLength: 0 },
  visible: {
    opacity: 1,
    pathLength: 1,
    transition: {
      opacity: { duration: 0.2 },
      pathLength: { duration: 0.95, ease: "easeInOut" },
    },
  },
  hover: {
    opacity: 1,
    pathLength: [1, 0.88, 1],
    transition: { duration: 2.4, ease: "easeInOut", repeat: Infinity },
  },
};

const arrowVariants: Variants = {
  hidden: { opacity: 0, pathLength: 0 },
  visible: {
    opacity: 1,
    pathLength: 1,
    x: 0,
    transition: {
      opacity: { duration: 0.2, delay: 0.6 },
      pathLength: { duration: 0.4, delay: 0.6, ease: "easeOut" },
      x: { duration: 0.25, ease: "easeOut" },
    },
  },
  hover: {
    opacity: 1,
    pathLength: 1,
    x: [0, -2, 0],
    transition: { duration: 2.4, ease: "easeInOut", repeat: Infinity },
  },
};

const clockHandVariants: Variants = {
  hidden: { opacity: 0, pathLength: 0, rotate: -20 },
  visible: {
    opacity: 1,
    pathLength: 1,
    rotate: 0,
    transition: {
      opacity: { duration: 0.2, delay: 0.65 },
      pathLength: { duration: 0.45, delay: 0.7, ease: "easeInOut" },
      rotate: { duration: 0.55, delay: 0.7, ease: "easeOut" },
    },
  },
  hover: {
    opacity: 1,
    pathLength: 1,
    rotate: [0, 16, 38, 38, 0],
    transition: { duration: 2.4, ease: "easeInOut", repeat: Infinity },
  },
};

/**
 * Runs its entrance animation once on mount, then loops while hovered.
 * Hover-out returns smoothly to the resting icon.
 */
export default function ChatHistory({
  width = 104,
  height = 104,
  style,
  animate,
  whileHover,
  ...props
}: ChatHistoryProps) {
  const reduceMotion = useReducedMotion();

  return (
    <motion.svg
      width={width}
      height={height}
      viewBox="0 0 104 104"
      fill="none"
      xmlns="http://www.w3.org/2000/svg"
      role="img"
      aria-label="Chat history"
      {...props}
      initial={reduceMotion ? false : "hidden"}
      animate={animate}
      whileInView={
        animate !== undefined || reduceMotion ? undefined : "visible"
      }
      viewport={{ once: true, amount: 0.55 }}
      whileHover={
        whileHover !== undefined
          ? whileHover
          : reduceMotion
            ? undefined
            : "hover"
      }
      style={{
        display: "inline-block",
        overflow: "visible",
        cursor: "pointer",
        ...style,
      }}
    >
      {/* A transparent hit area enables hovering anywhere in the 104x104 icon. */}
      <rect width="104" height="104" fill="transparent" />

      {/* Circular history arrow: original SVG path preserved. */}
      <motion.path
        d="M13 52a39 39 0 1 0 39-39 42.25 42.25 0 0 0-29.207 11.873L13 34.667"
        stroke="#a184f2"
        strokeWidth={9.167}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={outlineVariants}
      />

      {/* Arrow tip: separated from the original combined path for animation. */}
      <motion.path
        d="M13 13v21.667h21.667"
        stroke="#a184f2"
        strokeWidth={9.167}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={arrowVariants}
      />

      {/* Clock hands: rotate subtly around the clock's center on hover. */}
      <motion.path
        d="M52 30.332v21.667l17.333 8.666"
        stroke="#a184f2"
        strokeWidth={9.167}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={clockHandVariants}
        style={{ transformOrigin: "52px 52px", transformBox: "view-box" }}
      />
    </motion.svg>
  );
}
