import { externalApi } from '../../_api.ts';
import { db } from '@db/database.ts';

export const GET = externalApi({
  auth: true,
  handler: async ({ params }) => {
    const uuid = params['uuid']!;

    const blockedUser = await db.getBlockedUserByUuid({ uuid });
    if (blockedUser) return new Response(null, { status: 403 });

    const user = await db.getUserByUuid({ uuid });
    if (!user) return new Response(null, { status: 404 });

    const strikes = await db.getStrikesByUserId({ userId: user.id });

    return new Response(
      JSON.stringify({
        firstname: user.firstname,
        lastname: user.lastname,
        username: user.username,
        uuid: user.uuid,
        strikes: strikes.map((s) => ({ at: s.at.getTime(), weight: s.reason.weight }))
      }),
      { status: 200 }
    );
  }
});
