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

// Kept as a literal (not imported from `lib/session.ts`) on purpose: the
// Edge middleware runtime cannot bundle the Redis/Node-only code that
// `lib/session.ts` pulls in, so this file must stay dependency-free.
const SESSION_COOKIE_NAME = 'nexumi_session';

/**
 * Lightweight edge-safe guard: only checks that the session cookie exists.
 * The actual session and permission validation happens in
 * `requireAuthOrRedirect` / `requireGuildAccessOrRedirect` inside the
 * dashboard layouts, since that requires Redis/Prisma access which is not
 * available in the middleware runtime.
 */
export function middleware(request: NextRequest) {
  const hasSession = request.cookies.has(SESSION_COOKIE_NAME);

  if (!hasSession) {
    const loginUrl = new URL('/login', request.url);
    return NextResponse.redirect(loginUrl);
  }

  return NextResponse.next();
}

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