'use client';

import { usePathname } from 'next/navigation';
import { motion, useReducedMotion } from 'motion/react';
import { pageTransition } from '@/lib/motion';

/**
 * Page enter animation only — no AnimatePresence exit.
 * Exit + PopChildMeasure reserved the previous route's height and caused
 * huge empty scroll space on shorter pages (e.g. create user after users list).
 */
export function PageTransition({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();
  const reduceMotion = useReducedMotion();

  if (reduceMotion) {
    return <div className="p-6 lg:p-8">{children}</div>;
  }

  return (
    <motion.div
      key={pathname}
      initial={{ opacity: 0, y: 8 }}
      animate={{ opacity: 1, y: 0 }}
      transition={pageTransition}
      className="p-6 lg:p-8"
    >
      {children}
    </motion.div>
  );
}
