'use client';
import Link from 'next/link';import Image from 'next/image';
import { BrandIcon } from '@/components/brand-icon';
export { BrandIcon } from '@/components/brand-icon';
import {useTheme} from 'next-themes';import {useState,useEffect} from 'react';import {Sun,Moon,ArrowUpRight,Heart,ArrowRight,Clock3,Lock,Star,ExternalLink} from 'lucide-react';
import type{LarpTemplate}from '@/lib/catalog';import{Badge}from '@/components/ui/badge';import{Button}from '@/components/ui/button';import{useAccount}from '@/components/providers';import{cn}from '@/lib/utils';
import {isFreeDemoApp} from '@/lib/free-demo';
export function Logo({compact=false}:{compact?:boolean}){return <Link href="/" className="flex items-center gap-2.5 shrink-0 group"><Image alt="Larpdevice emblem" src="/logo.svg" width={36} height={36} className="drop-shadow-[0_0_15px_rgba(161,64,255,.4)] group-hover:scale-105 transition-transform"/><span className={cn('font-black tracking-[-.065em] text-xl md:text-2xl italic',compact&&'hidden md:inline')}>Larpdevice<span className="text-[9px] align-top ml-1 not-italic tracking-normal text-muted-foreground">TM</span></span></Link>}
export function ThemeToggle(){const {resolvedTheme,setTheme}=useTheme();const[mounted,setMounted]=useState(false);useEffect(()=>setMounted(true),[]);return <Button aria-label="Toggle light and dark mode" title="Toggle light/dark" variant="outline" size="icon" className="rounded-xl" onClick={()=>setTheme(resolvedTheme==='dark'?'light':'dark')}>{mounted&&resolvedTheme==='dark'?<Sun size={17}/>:<Moon size={17}/>}</Button>}
export function StatusBadge({status}:{status:LarpTemplate['status']}){return status==='coming-soon'?<Badge variant="warning">Coming Soon</Badge>:status==='early'?<Badge variant="premium">Early</Badge>:status==='restricted'?<Badge variant="outline">Info only</Badge>:<Badge variant="success">Available</Badge>}
export function LarpTile({template,compact=false,iconsOnly=false}:{template:LarpTemplate;compact?:boolean;iconsOnly?:boolean}){
 const {favorites,favorite}=useAccount();const available=Boolean(template.uiReady&&['available','early'].includes(template.status));
 if(iconsOnly)return <div className="group card-hover relative min-h-[156px] rounded-2xl border border-border bg-card/85 hover:border-primary/40"><Link href={`/larp/${template.slug}`} target="_blank" rel="noopener noreferrer" className="flex h-full min-h-[156px] flex-col items-center justify-center gap-3 p-5 text-center" aria-label={`Open ${template.name}`}><BrandIcon template={template} size={72}/><span className="font-bold text-sm leading-tight">{template.name}</span>{isFreeDemoApp(template.slug)&&<span className="text-[10px] font-bold tracking-wide text-amber-500">FREE DEMO · SIMULATION</span>}</Link><button aria-label={favorites.includes(template.slug)?'Remove from favorites':'Add to favorites'} onClick={()=>void favorite(template.slug)} className="absolute right-2 top-2 rounded-full bg-card/90 p-2 text-muted-foreground hover:text-pink-500"><Heart size={15} className={favorites.includes(template.slug)?'fill-pink-500 text-pink-500':''}/></button></div>;
 return <div className="group card-hover relative flex flex-col rounded-2xl border border-border bg-card/85 overflow-hidden min-w-0">
  {!iconsOnly&&<Link href={`/larp/${template.slug}`} target="_blank" rel="noopener noreferrer" className="block relative bg-muted/40 overflow-hidden aspect-[5/4]">
   {(available||Boolean(template.thumbnail))?<img src={template.thumbnail||`/app-thumbnails/${template.slug}.webp`} loading="lazy" alt={`${template.name} original saved UI preview`} className="w-full h-full object-cover object-top group-hover:scale-[1.02] transition-transform duration-300"/>:<div className="h-full grid place-items-center"><BrandIcon template={template} size={65}/></div>}
   {available&&<span className="absolute left-2 bottom-2 text-[9px] font-bold text-white bg-black/70 rounded px-2 py-1">LOCAL UI · SIMULATION</span>}
  </Link>}
  <button aria-label={favorites.includes(template.slug)?'Remove from favorites':'Add to favorites'} onClick={()=>void favorite(template.slug)} className="absolute right-3 top-3 text-muted-foreground bg-card/95 p-2 rounded-full hover:text-pink-500 z-10"><Heart size={16} className={favorites.includes(template.slug)?'fill-pink-500 text-pink-500':''}/></button>
  <div className="p-4 flex flex-col flex-1 min-w-0 gap-3"><Link href={`/larp/${template.slug}`} target="_blank" rel="noopener noreferrer" className="flex items-center gap-2 min-w-0"><BrandIcon template={template} size={iconsOnly?56:32}/><span className="min-w-0"><span className="font-bold text-sm truncate block">{template.name}</span><span className="text-xs text-muted-foreground truncate block">{template.category}</span></span></Link>
   {!compact&&<div className="flex items-center justify-between gap-2 mt-auto"><StatusBadge status={template.status}/><Link href={`/preview/${template.slug}`} target="_blank" rel="noopener noreferrer" className="text-primary text-xs font-bold flex items-center gap-1">Preview <ArrowUpRight size={13}/></Link></div>}
  </div>
 </div>
}
export function EmptyState({icon:Icon=Star,title,description,action,href='/library'}:{icon?:typeof Star;title:string;description:string;action?:string;href?:string}){return <div className="glass-panel px-6 py-14 text-center flex flex-col items-center"><div className="p-4 rounded-2xl bg-primary/15 text-primary"><Icon size={28}/></div><h3 className="text-xl font-bold mt-4">{title}</h3><p className="max-w-md text-sm text-muted-foreground mt-2 mb-5">{description}</p>{action&&<Button variant="gradient" asChild><Link href={href}>{action}<ArrowRight size={16}/></Link></Button>}</div>}
export function FictionalNotice({short=false}:{short?:boolean}){return <div className="flex items-center gap-2 text-[11px] text-muted-foreground"><Lock size={12}/><span>{short?'Fictional roleplay preview':'Fictional demo only · No real money, accounts, credentials, or transactions'}</span></div>}
