"use client";

import { useRef } from "react";
import Image from "next/image";
import { motion, useScroll, useTransform } from "framer-motion";

/** Imagen que se descubre con una cortina al entrar en pantalla y tiene un leve parallax. */
export default function RevealImage({
  src,
  alt,
  sizes,
  className = "",
  imgClassName = "",
  parallax = 8,
  delay = 0,
  children,
}: {
  src: string;
  alt: string;
  sizes: string;
  className?: string;
  imgClassName?: string;
  parallax?: number;
  delay?: number;
  children?: React.ReactNode;
}) {
  const ref = useRef<HTMLDivElement>(null);
  const { scrollYProgress } = useScroll({ target: ref, offset: ["start end", "end start"] });
  const y = useTransform(scrollYProgress, [0, 1], [`-${parallax}%`, `${parallax}%`]);

  // El disparador va en el contenedor externo (sin clip-path) para que el navegador lo detecte en pantalla.
  return (
    <motion.div
      ref={ref}
      className={`relative ${className}`}
      initial="hidden"
      whileInView="show"
      viewport={{ once: true, amount: 0.15 }}
    >
      <motion.div
        className="absolute inset-0 overflow-hidden bg-moss"
        variants={{ hidden: { clipPath: "inset(100% 0% 0% 0%)" }, show: { clipPath: "inset(0% 0% 0% 0%)" } }}
        transition={{ duration: 1.4, delay, ease: [0.76, 0, 0.24, 1] }}
      >
        <motion.div className="absolute -inset-y-[12%] inset-x-0" style={{ y }}>
          <motion.div
            className="relative h-full w-full"
            variants={{ hidden: { scale: 1.25 }, show: { scale: 1 } }}
            transition={{ duration: 1.8, delay, ease: [0.22, 1, 0.36, 1] }}
          >
            <Image src={src} alt={alt} fill sizes={sizes} className={`object-cover ${imgClassName}`} />
          </motion.div>
        </motion.div>
        {children}
      </motion.div>
    </motion.div>
  );
}
