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

export async function GET(request: NextRequest, {params}: {params: {user: string}}) {
  const id = params.user;
  if (id == null) return new Response('', {
    status: 400
  });
  let user = await db
    .selectFrom('users')
    .where('users.id', '=', id)
    .select('id')
    .executeTakeFirst();

  if (user == null) return new Response('', {
    status: 404
  });

  const until = parseInt(request.nextUrl.searchParams.get("until") || Date.now().toString());
  let judgement = await db
    .selectFrom('judgements')
    .where('judgements.author_id', '=', id)
    .where('judgements.published', '<', until)
    .limit(20)
    .orderBy('judgements.published', 'desc')
    .selectAll()
    .execute();

  if (judgement == null) return new Response('', {
    status: 500
  });

  return Response.json(judgement);
}