import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export default function proxy(request: NextRequest) {
  // Mengekstrak HttpOnly Cookie dari Pilar 1
  const token = request.cookies.get('jumpa_token')?.value;

  if (!token) {
    const origin = request.nextUrl.origin;
    // Spektakuler: Redirect ke Root (IAM) tanpa polusi URL bahasa
    return NextResponse.redirect(`${origin}/`);
  }

  // Jika ada, izinkan masuk ke ruangan Video Conference
  return NextResponse.next();
}

export const config = {
  matcher: [
    // Terapkan ke rute halaman yang membutuhkan autentikasi
    // Kecualikan: API, aset statis, supreme-eye (dashboard monitoring), dan sandbox
    '/((?!api|_next/static|_next/image|favicon.ico|supreme-eye|room/sandbox).*)',
  ],
};
