import { db } from "@/lib/mastoauth/kysely";
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 jams = await db
    .selectFrom('jams')
    .where('jams.created', '<', until)
    .where('jams.author_id', '=', id)
    .limit(20)
    .orderBy('jams.created', 'desc')
    .selectAll()
    .execute();

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

  return Response.json(jams);
}