"use client";

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

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

const draw = (delay: number, duration = 0.75): Variants => ({
  hidden: { opacity: 0, pathLength: 0 },
  visible: {
    opacity: 1,
    pathLength: 1,
    transition: {
      opacity: { duration: 0.2, delay },
      pathLength: { duration, delay, ease: "easeInOut" },
    },
  },
  hover: { opacity: 1, pathLength: 1 },
});

const clock: Variants = {
  hidden: { opacity: 0, pathLength: 0, scale: 0.85 },
  visible: {
    opacity: 1,
    pathLength: 1,
    scale: 1,
    transition: {
      opacity: { duration: 0.2, delay: 0.45 },
      pathLength: { duration: 0.75, delay: 0.45, ease: "easeInOut" },
      scale: { type: "spring", stiffness: 180, damping: 15 },
    },
  },
  hover: {
    opacity: 1,
    pathLength: 1,
    scale: [1, 1.045, 1],
    transition: { duration: 2.4, repeat: Infinity, ease: "easeInOut" },
  },
};

const hands: Variants = {
  hidden: { opacity: 0, pathLength: 0, rotate: -25 },
  visible: {
    opacity: 1,
    pathLength: 1,
    rotate: 0,
    transition: {
      opacity: { duration: 0.2, delay: 1.1 },
      pathLength: { duration: 0.35, delay: 1.1 },
      rotate: { duration: 0.5, ease: "easeOut" },
    },
  },
  hover: {
    opacity: 1,
    pathLength: 1,
    rotate: [0, 30, 0],
    transition: { duration: 2.4, repeat: Infinity, ease: "easeInOut" },
  },
};

/** Draws the clipboard and clock once; the clock ticks gently while hovered. */
export default function AgentPerformance({
  width = 104,
  height = 104,
  style,
  animate,
  whileHover,
  ...props
}: IconProps) {
  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="Agent performance"
      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 }}
    >
      <motion.path
        d="M69.333 17.332H78a8.667 8.667 0 0 1 8.666 8.667v3.605m-52-12.272H26a8.667 8.667 0 0 0-8.667 8.667v60.666A8.667 8.667 0 0 0 26 95.332h8.666"
        stroke="#64bc4e"
        strokeWidth={7.667}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={draw(0)}
      />
      <motion.path
        d="M39 8.668h26a4.333 4.333 0 0 1 4.334 4.333v8.667A4.333 4.333 0 0 1 65 26.001H39a4.333 4.333 0 0 1-4.333-4.333v-8.667A4.333 4.333 0 0 1 39 8.668"
        stroke="#64bc4e"
        strokeWidth={7.667}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={draw(0.2, 0.55)}
      />
      <motion.path
        d="M95.333 69.334c0 14.36-11.64 26-26 26s-26-11.64-26-26 11.64-26 26-26 26 11.64 26 26"
        stroke="#64bc4e"
        strokeWidth={7.667}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={clock}
        style={{ transformBox: "fill-box", transformOrigin: "center" }}
      />
      <motion.path
        d="M69.333 60.666v9.533l6.933 4.334"
        stroke="#64bc4e"
        strokeWidth={7.667}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={hands}
        style={{ transformOrigin: "69.333px 69.334px" }}
      />
    </motion.svg>
  );
}
