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

export function middleware(request: NextRequest) {
  const { pathname } = request.nextUrl;

  // Protect /dashboard and /mira (login required; mira 額外白名單檢查在 layout)
  if (pathname.startsWith('/dashboard') || pathname.startsWith('/mira')) {
    const session = request.cookies.get('arcrun_session');
    if (!session?.value) {
      const loginUrl = new URL('/login', request.url);
      loginUrl.searchParams.set('redirect', pathname);
      return NextResponse.redirect(loginUrl);
    }
  }

  return NextResponse.next();
}

export const config = {
  matcher: ['/dashboard', '/dashboard/:path*', '/mira', '/mira/:path*'],
};
