import { NextResponse } from "next/server";
import { cookies } from "next/headers";

const BE_BASE_URL = process.env.BE_BASE_URL || "http://localhost:3000";
const COOKIE_NAME = "auth_token";

export async function DELETE() {
  const cookieStore = await cookies();
  const token = cookieStore.get(COOKIE_NAME)?.value;

  if (token) {
    try {
      await fetch(`${BE_BASE_URL}/logout`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`, // satisfy requireJwt
        },
        body: JSON.stringify({ token }), // satisfy body check
      });
    } catch (err) {
      console.error("BE /logout failed:", err);
    }
  }

  cookieStore.delete(COOKIE_NAME);
  return NextResponse.json({ success: true, message: "Logged out" });
}
