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

export async function POST() {
  try {
    const cookieStore = await cookies();
    const sessionToken = cookieStore.get("tia_session")?.value;

    if (sessionToken) {
      await sql`
        DELETE FROM sessions WHERE session_token = ${sessionToken}
      `;
    }

    const response = NextResponse.json({ success: true });
    response.cookies.set("tia_session", "", {
      httpOnly: true,
      secure: process.env.NODE_ENV === "production",
      sameSite: "lax",
      maxAge: 0,
      path: "/",
    });

    return response;
  } catch (error) {
    return NextResponse.json({ error: String(error) }, { status: 500 });
  }
}