"use client"

import { useEffect, useState } from "react"
import { Download, Share, SquarePlus, X } from "lucide-react"
import {
  clearDeferredInstallPrompt,
  getDeferredInstallPrompt,
  subscribeInstallPrompt,
  type BeforeInstallPromptEvent,
} from "@/lib/pwa-install"

const DISMISS_KEY = "teecup-install-dismissed-until"
const DISMISS_COOLDOWN_DAYS = 14

function isStandalone() {
  if (typeof window === "undefined") return false
  const nav = window.navigator as Navigator & { standalone?: boolean }
  return window.matchMedia("(display-mode: standalone)").matches || nav.standalone === true
}

function isIOS() {
  if (typeof window === "undefined") return false
  return /iphone|ipad|ipod/i.test(window.navigator.userAgent)
}

function isDismissedForNow() {
  if (typeof window === "undefined") return true
  const raw = window.localStorage.getItem(DISMISS_KEY)
  if (!raw) return false
  const until = Number(raw)
  return Number.isFinite(until) && Date.now() < until
}

function dismissForNow() {
  const until = Date.now() + DISMISS_COOLDOWN_DAYS * 24 * 60 * 60 * 1000
  window.localStorage.setItem(DISMISS_KEY, String(until))
}

// PWA-installasjonsoppfordring (FEATURE_BACKLOG.md "PWA-installasjon").
// To distinkte varianter -- INGEN visning for andre nettlesere uten en
// reell installasjonsvei:
// - Android/Chrome-familien: ekte "Installer"-knapp via det fangede
//   beforeinstallprompt-eventet (lib/pwa-install.ts).
// - iOS Safari: Apple har ALDRI implementert beforeinstallprompt -- kun et
//   instruksjonsbanner (Del-ikon -> "Legg til på Hjemskjerm") er mulig.
export function InstallPrompt() {
  const [dismissed, setDismissed] = useState(true)
  const [platform, setPlatform] = useState<"android" | "ios" | "none">("none")
  const [deferredEvent, setDeferredEvent] = useState<BeforeInstallPromptEvent | null>(null)

  useEffect(() => {
    if (isStandalone() || isDismissedForNow()) return
    if (isIOS()) {
      setPlatform("ios")
      setDismissed(false)
      return
    }
    // Android/Chrome-familien: vis kun når/hvis eventet faktisk fyres --
    // andre nettlesere uten en reell installasjonsvei skal ikke vise noe.
    const existing = getDeferredInstallPrompt()
    if (existing) {
      setDeferredEvent(existing)
      setPlatform("android")
      setDismissed(false)
    }
    const unsubscribe = subscribeInstallPrompt((event) => {
      if (event) {
        setDeferredEvent(event)
        setPlatform("android")
        setDismissed(false)
      }
    })
    return unsubscribe
  }, [])

  if (dismissed || platform === "none") return null

  async function handleInstall() {
    if (!deferredEvent) return
    await deferredEvent.prompt()
    await deferredEvent.userChoice
    clearDeferredInstallPrompt()
    setDismissed(true)
  }

  function handleDismiss() {
    dismissForNow()
    setDismissed(true)
  }

  // "Forest Green"-visuell redesign (2026-08-01) -- samme palett som
  // dashboard.tsx/login-form.tsx sin C-konstant, inline-anvendt siden dette
  // er den eneste komponenten som trenger den. All logikk over uendret.
  return (
    <div
      role="region"
      aria-label="Installer TeeCup"
      className="relative overflow-hidden rounded-2xl p-4 sm:p-5"
      style={{
        background: "linear-gradient(180deg, #1a4325 0%, #123019 100%)",
        boxShadow: "0 18px 40px -22px rgba(1, 44, 17, 0.5)",
      }}
    >
      <div className="flex items-start gap-3.5">
        <span className="flex size-11 shrink-0 items-center justify-center rounded-xl" style={{ backgroundColor: "rgba(255,255,255,0.12)" }}>
          {platform === "ios" ? (
            <Share aria-hidden="true" className="size-5" color="#bfeec4" />
          ) : (
            <Download aria-hidden="true" className="size-5" color="#bfeec4" />
          )}
        </span>

        <div className="flex min-w-0 flex-1 flex-col gap-3">
          <div className="flex flex-col gap-1 pr-6">
            <h2 className="text-base font-bold text-balance" style={{ color: "#ffffff" }}>
              Legg TeeCup til på hjemskjermen
            </h2>
            {platform === "ios" ? (
              <p className="text-sm leading-relaxed text-pretty" style={{ color: "#bfeec4" }}>
                Trykk <Share aria-hidden="true" className="inline size-4 -translate-y-0.5" /> (Del) nederst i
                Safari, velg deretter <SquarePlus aria-hidden="true" className="inline size-4 -translate-y-0.5" />{" "}
                «Legg til på Hjem-skjerm».
              </p>
            ) : (
              <p className="text-sm leading-relaxed text-pretty" style={{ color: "#bfeec4" }}>
                Rask tilgang, fungerer som en egen app, og virker delvis uten nett.
              </p>
            )}
          </div>

          <div className="flex items-center gap-2">
            {platform === "android" && (
              <button
                type="button"
                onClick={handleInstall}
                className="inline-flex min-h-[44px] items-center gap-1.5 rounded-xl px-4 text-sm font-bold transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white"
                style={{ backgroundColor: "#ffffff", color: "#012c11" }}
              >
                <Download aria-hidden="true" className="size-4" />
                Installer
              </button>
            )}
            <button
              type="button"
              onClick={handleDismiss}
              className="inline-flex min-h-[44px] items-center rounded-xl px-3 text-sm font-semibold transition-colors hover:bg-white/10 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white"
              style={{ color: "#bfeec4" }}
            >
              Ikke nå
            </button>
          </div>
        </div>
      </div>

      <button
        type="button"
        onClick={handleDismiss}
        aria-label="Lukk installasjonsoppfordringen"
        className="absolute right-2 top-2 flex size-9 items-center justify-center rounded-lg transition-colors hover:bg-white/10 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white"
        style={{ color: "#bfeec4" }}
      >
        <X aria-hidden="true" className="size-4" />
      </button>
    </div>
  )
}
