import { AUTH_COOKIE_NAME, BE_BASE_URL } from "@/app/services/constants";
import { NextResponse } from "next/server";
import { cookies } from "next/headers";

export async function DELETE() {
  const cookieStore = await cookies();
  const token = cookieStore.get(AUTH_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(AUTH_COOKIE_NAME);
  return NextResponse.json({ success: true, message: "Logged out" });
}
