import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

export function middleware(req: NextRequest) {
  const adminCookie =
    req.cookies.get("admin")?.value;

  const pathname = req.nextUrl.pathname;

  // ✅ autoriser la page login
  if (pathname === "/admin/login") {
    return NextResponse.next();
  }

  // ✅ protéger admin
  if (
    pathname.startsWith("/admin") &&
    adminCookie !== "authenticated"
  ) {
    return NextResponse.redirect(
      new URL("/admin/login", req.url)
    );
  }

  return NextResponse.next();
}

export const config = {
  matcher: ["/admin/:path*"],
};