"use client";

import { motion, useReducedMotion } from "framer-motion";
import type { SynopStation, WeatherMood } from "@/types/imgw";

const rainDrops = Array.from({ length: 32 }, (_, index) => ({
  left: `${(index * 29 + 7) % 100}%`,
  delay: (index % 11) * 0.13,
  duration: 0.72 + (index % 5) * 0.12,
  height: 12 + (index % 4) * 4,
}));

const windLines = Array.from({ length: 7 }, (_, index) => ({
  top: `${18 + index * 11}%`,
  delay: index * 0.22,
  width: 70 + (index % 3) * 34,
}));

const stars = Array.from({ length: 16 }, (_, index) => ({
  left: `${(index * 37 + 11) % 96}%`,
  top: `${(index * 23 + 8) % 72}%`,
  delay: (index % 6) * 0.35,
}));

export function WeatherEffects({ station, mood }: { station: SynopStation; mood: WeatherMood }) {
  const reduceMotion = useReducedMotion();
  const rainfall = station.rainfall ?? 0;
  const isRaining = rainfall >= 0.1;
  const visibleDrops = rainfall >= 5 ? rainDrops : rainDrops.slice(0, 18);
  const isWindy = (station.windSpeed ?? 0) >= 8;

  return (
    <div aria-hidden="true" className="pointer-events-none absolute inset-0 overflow-hidden">
      {mood === "warm" && (
        <motion.div
          animate={reduceMotion ? undefined : { scale: [1, 1.08, 1], opacity: [0.4, 0.58, 0.4] }}
          transition={{ duration: 7, repeat: Infinity, ease: "easeInOut" }}
          className="absolute -right-16 -top-20 size-64 rounded-full bg-amber-200/45 blur-3xl"
        />
      )}
      {mood === "night" && stars.map((star, index) => (
        <motion.span
          key={index}
          animate={reduceMotion ? undefined : { opacity: [0.2, 0.75, 0.2] }}
          transition={{ duration: 2.4, delay: star.delay, repeat: Infinity, ease: "easeInOut" }}
          className="absolute size-1 rounded-full bg-white/75"
          style={{ left: star.left, top: star.top }}
        />
      ))}
      {isWindy && windLines.map((line, index) => (
        <motion.span
          key={index}
          initial={{ x: "-30%", opacity: 0 }}
          animate={reduceMotion ? { opacity: 0.28 } : { x: ["-30%", "135%"], opacity: [0, 0.35, 0] }}
          transition={{ duration: 3.4, delay: line.delay, repeat: Infinity, ease: "linear" }}
          className="absolute h-px rounded-full bg-white/60"
          style={{ top: line.top, width: line.width }}
        />
      ))}
      {isRaining && !reduceMotion && visibleDrops.map((drop, index) => (
        <motion.span
          key={index}
          initial={{ y: "-8vh", opacity: 0 }}
          animate={{ y: ["-8vh", "85vh"], x: [0, -12], opacity: [0, 0.5, 0.12] }}
          transition={{ duration: drop.duration, delay: drop.delay, repeat: Infinity, ease: "linear" }}
          className="absolute -top-8 w-px rounded-full bg-cyan-100/80"
          style={{ height: drop.height, left: drop.left, transform: "rotate(14deg)" }}
        />
      ))}
      {mood === "cold" && (
        <div className="absolute inset-x-0 bottom-0 h-28 bg-gradient-to-t from-cyan-100/20 to-transparent" />
      )}
    </div>
  );
}
