import {headers} from 'next/headers';
import {NextRequest} from 'next/server';
import {redirect,notFound} from 'next/navigation';
import {sessionUser,getProject} from '@/lib/server-store';
import {activeLiveLink,issueLiveLink} from '@/lib/live-links';
import {isStudioAdmin} from '@/lib/studio-store';
import {canUseApp} from '@/lib/billing-store';
export const dynamic='force-dynamic';
/** One reliable, authenticated destination for every Open Live LARP action. */
export default async function OpenSavedLarp({params}:{params:Promise<{id:string}>}){
 const {id}=await params;
 if(!/^[a-f0-9-]{32,40}$/i.test(id))notFound();
 const h=await headers();
 const req=new NextRequest(process.env.APP_URL||'http://localhost:3000',{headers:h});
 const user=sessionUser(req);
 if(!user)redirect('/login?next='+encodeURIComponent('/open/'+id));
 const project=getProject(id);
 if(!project||project.userId!==user.id)notFound();
 if(!isStudioAdmin(req)&&!canUseApp(user.id,project.templateSlug))redirect('/pricing');
 const live=activeLiveLink(id,user.id)||issueLiveLink(id,user.id,project.templateSlug);
 redirect(live.url);
}
