"use client";

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

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

// Runs once on mount, then loops only while the SVG is hovered.
const mainSparkle: Variants = {
  hidden: {
    opacity: 0,
    pathLength: 0,
    scale: 0.65,
    rotate: -15,
  },
  visible: {
    opacity: 1,
    pathLength: 1,
    scale: 1,
    rotate: 0,
    transition: {
      pathLength: { duration: 0.8, ease: "easeInOut" },
      opacity: { duration: 0.3 },
      scale: {
        type: "spring",
        stiffness: 220,
        damping: 14,
        delay: 0.15,
      },
      rotate: { duration: 0.7, ease: "easeOut" },
    },
  },
  hover: {
    opacity: 1,
    pathLength: 1,
    scale: [1, 1.12, 0.96, 1],
    rotate: [0, 5, -3, 0],
    transition: {
      duration: 1.8,
      ease: "easeInOut",
      repeat: Infinity,
      repeatType: "loop",
    },
  },
};

const accentSparkle: Variants = {
  hidden: {
    opacity: 0,
    pathLength: 0,
    scale: 0,
    rotate: -40,
  },
  visible: {
    opacity: 1,
    pathLength: 1,
    scale: 1,
    rotate: 0,
    transition: {
      pathLength: {
        duration: 0.6,
        delay: 0.35,
        ease: "easeInOut",
      },
      opacity: { duration: 0.3, delay: 0.35 },
      scale: {
        type: "spring",
        stiffness: 320,
        damping: 13,
        delay: 0.4,
      },
      rotate: { duration: 0.65, delay: 0.35 },
    },
  },
  hover: {
    opacity: [1, 0.65, 1, 1],
    pathLength: 1,
    scale: [1, 0.75, 1.2, 1],
    rotate: [0, -12, 10, 0],
    transition: {
      duration: 1.8,
      delay: 0.2,
      ease: "easeInOut",
      repeat: Infinity,
      repeatType: "loop",
    },
  },
};

export default function AIPoweredInbox({
  width = 104,
  height = 104,
  style,
  animate,
  whileHover,
  ...props
}: AIPoweredInboxProps) {
  const reducedMotion = useReducedMotion();

  return (
    <motion.svg
      role="img"
      aria-label="AI-powered inbox"
      {...props}
      width={width}
      height={height}
      viewBox="0 0 104 104"
      fill="none"
      xmlns="http://www.w3.org/2000/svg"
      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",
        cursor: "pointer",
        ...style,
      }}
    >
      {/* Main star — same SVG geometry and styling as the original. */}
      <motion.path
        d="M47.8 78a3.682 3.682 0 0 1-7.239 0 36.82 36.82 0 0 0-30.2-30.2 3.682 3.682 0 0 1 0-7.239 36.82 36.82 0 0 0 30.2-30.2 3.682 3.682 0 0 1 7.239 0A36.82 36.82 0 0 0 78 40.56a3.682 3.682 0 0 1 0 7.239A36.82 36.82 0 0 0 47.8 78"
        stroke="#64bc4e"
        strokeWidth={9.417}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={mainSparkle}
        style={{ transformBox: "fill-box", transformOrigin: "center" }}
      />

      {/* Accent star. No clip path so it never gets cut off while scaling. */}
      <motion.path
        d="M89.642 100.332a1.305 1.305 0 0 1-2.562 0 13.03 13.03 0 0 0-10.69-10.69 1.303 1.303 0 0 1 0-2.562 13.03 13.03 0 0 0 10.69-10.691 1.303 1.303 0 0 1 2.562 0 13.03 13.03 0 0 0 10.691 10.69 1.3 1.3 0 0 1 1.064 1.282 1.305 1.305 0 0 1-1.064 1.28 13.03 13.03 0 0 0-10.691 10.691"
        stroke="#64bc4e"
        strokeWidth={5}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={accentSparkle}
        style={{ transformBox: "fill-box", transformOrigin: "center" }}
      />
    </motion.svg>
  );
}
