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

export function middleware(request: NextRequest): NextResponse {
  // Check if the request is for a protected route
  const protectedRoutes = ['/review', '/credentials', '/metrics', '/audit'];
  const isProtectedRoute = protectedRoutes.some((route) => request.nextUrl.pathname.startsWith(route));

  if (isProtectedRoute) {
    // Check for auth token in cookies or headers
    const token = request.cookies.get('auth_token') || request.headers.get('authorization');

    if (!token) {
      // Redirect to login if not authenticated
      const loginUrl = new URL('/login', request.url);
      loginUrl.searchParams.set('redirect', request.nextUrl.pathname);
      return NextResponse.redirect(loginUrl);
    }
  }

  return NextResponse.next();
}

export const config = {
  matcher: ['/review/:path*', '/credentials/:path*', '/metrics/:path*', '/audit/:path*'],
};

