"use client";

import * as React from "react";
import { useSession } from "next-auth/react";

/**
 * Web apps cannot reliably block OS-level capture (Snipping Tool, Win+Shift+S, etc.): Windows
 * often handles those before the page sees keydown. We still call preventDefault in capture
 * phase for best-effort mitigation when the browser does deliver the event.
 */
export function DashboardScreenshotDeterrent() {
  const { status } = useSession();

  React.useLayoutEffect(() => {
    if (status !== "authenticated") {
      return;
    }
    const blockCaptureKeys = (e: KeyboardEvent) => {
      // PrintScreen (Windows)
      if (e.key === "PrintScreen") {
        e.preventDefault();
        return;
      }
      // Cmd+Shift+4 / Cmd+Shift+3 (macOS)
      if (
        e.metaKey &&
        e.shiftKey &&
        (e.code === "Digit3" || e.code === "Digit4" || e.code === "Digit5")
      ) {
        e.preventDefault();
        return;
      }
      // Cmd+Shift+S (macOS) or Ctrl+Shift+S (Windows)
      if (
        e.shiftKey &&
        (e.metaKey || e.ctrlKey) &&
        (e.key === "s" || e.key === "S" || e.code === "KeyS")
      ) {
        e.preventDefault();
      }
    };

    const blockContextMenu = (e: MouseEvent) => e.preventDefault();
    const blockDragStart = (e: DragEvent) => e.preventDefault();

    window.addEventListener("keydown", blockCaptureKeys, true);
    window.addEventListener("contextmenu", blockContextMenu, true);
    window.addEventListener("dragstart", blockDragStart, true);
    return () => {
      window.removeEventListener("keydown", blockCaptureKeys, true);
      window.removeEventListener("contextmenu", blockContextMenu, true);
      window.removeEventListener("dragstart", blockDragStart, true);
    };
  }, [status]);

  return null;
}
