import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"

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

  // Don't protect the login page
  if (pathname === "/intern/login") {
    return NextResponse.next()
  }

  // Check for session cookie on all /intern/* routes
  const sessionCookie = request.cookies.get("session")

  if (!sessionCookie?.value) {
    const loginUrl = new URL("/intern/login", request.url)
    loginUrl.searchParams.set("redirect", pathname)
    return NextResponse.redirect(loginUrl)
  }

  return NextResponse.next()
}

export const config = {
  matcher: ["/intern/:path*"],
}
