import { createFileRoute, redirect } from "@tanstack/react-router";
import { isAllowedEmbedUrl } from "@/lib/embed/ad-guard";

export const Route = createFileRoute("/play")({
  validateSearch: (s: Record<string, unknown>) => ({
    u: typeof s.u === "string" ? s.u : "",
  }),
  beforeLoad: ({ search }) => {
    if (!search.u || !isAllowedEmbedUrl(search.u)) {
      throw redirect({ to: "/" });
    }
  },
  component: PlayShell,
});

function PlayShell() {
  const { u } = Route.useSearch();

  return (
    <div className="fixed inset-0 w-full h-full bg-black relative overflow-hidden">
      <iframe
        src={u}
        title="game"
        className="w-full h-full border-0"
        allow="autoplay; fullscreen; gamepad"
        allowFullScreen
        referrerPolicy="no-referrer-when-downgrade"
      />
    </div>
  );
}
