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

export async function POST() {
  const cookieStore = cookies();
  const allCookies = cookieStore.getAll();

  console.log("Clearing the following cookies:");

  // 获取当前请求的 host（服务器端）
  const hostHeader = headers().get("host");
  const currentHost = hostHeader || "localhost"; // 如果没有 host 信息，使用 "localhost"

  // Loop through all cookies and clear them
  allCookies.forEach(({ name }) => {
    console.log(`............Clearing cookie: ${name}`);
    try {
      cookieStore.set({
        name,
        value: "",
        path: "/",
        maxAge: 0,
        httpOnly: true,
        secure: true,
        sameSite: "lax",
        domain: currentHost,  // 使用服务器端获取的 host
      });
    } catch (error) {
      console.error(`Error clearing cookie ${name}:`, error);
    }
  });

  // Return success response using NextResponse
  return NextResponse.json({ message: "Cookies cleared successfully" }, { status: 200 });
}





// import { NextResponse } from 'next/server';

// export async function POST(req) {
//   try {
//     console.log("Clearing cookies...");

//     // Perform cookie clearing logic here

//     // Return success response using NextResponse
//     return NextResponse.json({ message: "Cookies cleared successfully" }, { status: 200 });
//   } catch (error) {
//     console.error("Error occurred while clearing cookies:", error);
    
//     // Return error response
//     return NextResponse.json({ error: "Internal server error" }, { status: 500 });
//   }
// }
