import { NextResponse } from "next/server";
import { cookies } from "next/headers";
import { getSessionCookieName, verifySessionToken } from "@/lib/session";

export async function requireApiAuth(): Promise<NextResponse | null> {
    const cookieStore = await cookies();
    const authCookie = cookieStore.get(getSessionCookieName());
    const session = authCookie ? await verifySessionToken(authCookie.value) : null;

    if (!session) {
        return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    return null;
}
