import { Link, useNavigate } from "@tanstack/react-router";
import { History, Home, ListChecks, Gift, Wallet, UserCircle2, Download, Shield, Send, Wrench } from "lucide-react";
import type { ReactNode } from "react";
import { useMe, useSettings, inr } from "@/lib/app";

const NAV = [
  { to: "/dashboard", label: "Home", icon: Home },
  { to: "/tasks", label: "Tasks", icon: ListChecks },
  { to: "/refer", label: "Refer", icon: Gift },
  { to: "/withdraw", label: "Withdraw", icon: Wallet },
  { to: "/history", label: "History", icon: History },
] as const;

export function AppShell({ children }: { children: ReactNode }) {
  const { data: me } = useMe();
  const { data: settings } = useSettings();
  const navigate = useNavigate();
  const blocked = me?.profile && (me.profile.is_blocked || !me.profile.is_active);
  const maintenance = settings?.maintenance_mode && !me?.isAdmin;

  return (
    <div className="min-h-screen pb-24 md:pb-8">
      <header className="sticky top-0 z-30 border-b border-border bg-background/80 backdrop-blur-xl">
        <div className="mx-auto flex max-w-5xl items-center gap-3 px-4 py-3">
          <Link to="/dashboard" className="flex items-center gap-2">
            <img src={settings?.logo_url || "/icons/icon-192.png"} alt="" className="h-8 w-8 rounded-lg" />
            <span className="font-display text-lg font-bold">{settings?.site_name ?? "RozTask"}</span>
          </Link>
          <nav className="ml-6 hidden gap-1 md:flex">
            {NAV.map((n) => (
              <Link key={n.to} to={n.to} className="rounded-lg px-3 py-1.5 text-sm text-muted-foreground hover:text-foreground" activeProps={{ className: "bg-primary/15 !text-primary" }}>
                {n.label}
              </Link>
            ))}
          </nav>
          <div className="ml-auto flex items-center gap-2">
            {me?.isAdmin && (
              <Link to="/admin" className="grid h-9 w-9 place-items-center rounded-full bg-accent/15 text-accent" aria-label="Admin panel"><Shield className="h-5 w-5" /></Link>
            )}
            
            <span className="hidden rounded-full bg-primary/15 px-3 py-1 text-sm font-bold text-primary sm:inline">{inr(me?.profile?.balance)}</span>
            <button onClick={() => navigate({ to: "/profile" })} className="grid h-9 w-9 place-items-center rounded-full bg-primary text-primary-foreground" aria-label="Profile">
              <UserCircle2 className="h-5 w-5" />
            </button>
          </div>
        </div>
      </header>
      <main className="mx-auto max-w-5xl px-4 py-5">
        {maintenance ? (
          <div className="glass-card mx-auto max-w-md p-8 text-center"><Wrench className="mx-auto mb-3 h-10 w-10 text-warning" /><h2 className="text-xl font-bold">Under maintenance</h2><p className="mt-2 text-sm text-muted-foreground">We'll be back shortly. Please check again later.</p></div>
        ) : (
          <>
            {blocked && <div className="mb-4 rounded-xl border border-destructive/40 bg-destructive/10 p-3 text-sm text-destructive">Your account is blocked or inactive. Contact support.</div>}
            {children}
          </>
        )}
        {settings?.telegram_link && (
          <a href={settings.telegram_link} target="_blank" rel="noreferrer" className="mt-8 flex items-center justify-center gap-2 text-sm text-info"><Send className="h-4 w-4" /> Join our Telegram channel</a>
        )}
      </main>
      <nav className="fixed inset-x-0 bottom-0 z-30 border-t border-border bg-card md:hidden">
        <div className="mx-auto grid max-w-md grid-cols-5">
          {NAV.map((n) => (
            <Link key={n.to} to={n.to} className="flex flex-col items-center gap-0.5 py-2.5 text-[11px] text-nav-inactive" activeProps={{ className: "!text-primary" }}>
              <n.icon className="h-5 w-5" />
              {n.label}
            </Link>
          ))}
        </div>
      </nav>
    </div>
  );
}
