"use client";

import { useEffect, useRef } from "react";
import { useRouter, usePathname } from "next/navigation";

export default function InactivityMonitor() {
  const router = useRouter();
  const pathname = usePathname();
  const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);

  useEffect(() => {
    if (typeof window === "undefined") return;

    // Only activate in PWA standalone mode
    const isStandalone =
      window.matchMedia("(display-mode: standalone)").matches ||
      window.matchMedia("(display-mode: fullscreen)").matches ||
      (window.navigator as unknown as { standalone?: boolean }).standalone === true;
    if (!isStandalone) return;

    // Skip on lockscreen/login pages
    if (pathname?.startsWith("/lockscreen") || pathname?.startsWith("/login") || pathname?.startsWith("/register")) return;

    // Read timeout from localStorage
    let timeoutMinutes = 5;
    try {
      const stored = localStorage.getItem("widget_config");
      if (stored) {
        const cfg = JSON.parse(stored);
        if (cfg.inactivityTimeout > 0) timeoutMinutes = cfg.inactivityTimeout;
      }
    } catch { /* ignore */ }

    const timeoutMs = timeoutMinutes * 60 * 1000;

    function resetTimer() {
      if (timerRef.current) clearTimeout(timerRef.current);
      timerRef.current = setTimeout(() => {
        router.push("/lockscreen");
      }, timeoutMs);
    }

    const events = ["mousedown", "mousemove", "keydown", "touchstart", "scroll", "click"];
    events.forEach(e => window.addEventListener(e, resetTimer, { passive: true }));
    resetTimer();

    return () => {
      if (timerRef.current) clearTimeout(timerRef.current);
      events.forEach(e => window.removeEventListener(e, resetTimer));
    };
  }, [pathname, router]);

  return null;
}
