import { NextRequest, NextResponse, NextFetchEvent } from 'next/server';
import { createSimplyScanBeacon } from './simplyscan-beacon.mjs';
const beacon=createSimplyScanBeacon({key:process.env.SIMPLYSCAN_INGEST_KEY!});
// Export the returned function as middleware after supplying a trusted IP adapter.
export function withSimplyScanMiddleware(clientIp:(r:NextRequest)=>string|null){return function middleware(req:NextRequest,event:NextFetchEvent){
 const response=NextResponse.next();
 // Replace this with your hosting provider's authenticated/trusted IP adapter.
 const ip=clientIp(req);
 if(ip){beacon.record({method:req.method,path:req.nextUrl.pathname,status:response.status,ip,userAgent:req.headers.get('user-agent')??''});event.waitUntil(beacon.flush().catch(()=>{}));}
 return response;
};}
// Middleware sees only its own response. Instrument auth route handlers to record
// actual login success/failure status and pseudonymous account ID for auth detection.
