import { createFileRoute, Link } from "@tanstack/react-router";
import { useEffect, useState } from "react";
import { Download, Share, PlusSquare, MoreVertical, CheckCircle2 } from "lucide-react";
import { Button } from "@/components/ui/button";

export const Route = createFileRoute("/download")({
  head: () => ({ meta: [{ title: "Download App – RozTask" }, { name: "description", content: "Install the RozTask app on your phone." }, { property: "og:title", content: "Download App – RozTask" }, { property: "og:description", content: "Install the RozTask app on your phone." }, { property: "og:type", content: "website" }, { name: "twitter:card", content: "summary" }] }),
  component: DownloadPage,
});

type BIP = Event & { prompt: () => Promise<void>; userChoice: Promise<{ outcome: string }> };

function DownloadPage() {
  const [evt, setEvt] = useState<BIP | null>(null);
  const [installed, setInstalled] = useState(false);
  const [ios, setIos] = useState(false);
  useEffect(() => {
    setIos(/iphone|ipad|ipod/i.test(navigator.userAgent));
    setInstalled(window.matchMedia("(display-mode: standalone)").matches);
    const h = (e: Event) => { e.preventDefault(); setEvt(e as BIP); };
    window.addEventListener("beforeinstallprompt", h);
    window.addEventListener("appinstalled", () => setInstalled(true));
    return () => window.removeEventListener("beforeinstallprompt", h);
  }, []);

  return (
    <div className="mx-auto max-w-md px-4 py-10">
      <div className="glass-card animate-rise p-8 text-center">
        <img src="/icons/icon-512.png" alt="RozTask app icon" className="mx-auto h-24 w-24 rounded-3xl shadow-glow" />
        <h1 className="mt-4 text-2xl font-extrabold">Download RozTask App</h1>
        <p className="mt-2 text-sm text-muted-foreground">Fast, light and works like a real app on your phone.</p>
        {installed ? (
          <p className="mt-6 flex items-center justify-center gap-2 text-primary"><CheckCircle2 className="h-5 w-5" /> App is installed</p>
        ) : evt ? (
          <Button variant="hero" size="lg" className="mt-6 w-full" onClick={async () => { await evt.prompt(); const r = await evt.userChoice; if (r.outcome === "accepted") setInstalled(true); setEvt(null); }}><Download /> Install App</Button>
        ) : (
          <div className="mt-6 space-y-3 text-left text-sm">
            <p className="font-semibold">Add to Home Screen:</p>
            {ios ? (
              <>
                <p className="flex items-center gap-2"><Share className="h-4 w-4 text-primary" /> 1. Tap the Share button in Safari</p>
                <p className="flex items-center gap-2"><PlusSquare className="h-4 w-4 text-primary" /> 2. Choose "Add to Home Screen"</p>
                <p>3. Tap "Add"</p>
              </>
            ) : (
              <>
                <p className="flex items-center gap-2"><MoreVertical className="h-4 w-4 text-primary" /> 1. Open the browser menu (⋮)</p>
                <p className="flex items-center gap-2"><PlusSquare className="h-4 w-4 text-primary" /> 2. Tap "Install app" or "Add to Home screen"</p>
                <p>3. Confirm — the app icon appears on your home screen</p>
              </>
            )}
            <p className="text-xs text-muted-foreground">Tip: Use Chrome on Android or Safari on iPhone. Open the published website (not inside another app).</p>
          </div>
        )}
        <Link to="/" className="mt-6 block text-sm text-primary">← Back</Link>
      </div>
    </div>
  );
}
