import { db } from "@/lib/mastoauth/kysely";
import { cookies } from "next/headers";
import { NextRequest } from "next/server";

export async function GET(request: NextRequest) {
  const cookieStore = cookies();
  const instance = cookieStore.get('instance')?.value;
  if (instance == null) return new Response('', {
    status: 400
  });

  // Delete token

  try {
    await db.deleteFrom('tokens')
      .where('tokens.id', '=', instance)
      .executeTakeFirstOrThrow();
  } catch (err) {
    return new Response('Something failed!', {
      status: 500
    });
  }

  cookieStore.delete("token");
  cookieStore.delete("instance");
  return Response.redirect(new URL('/jams', request.url));
}