import { externalApi } from '../../_api.ts';
import { db } from '@db/database.ts';
import { BASE_PATH } from 'astro:env/server';

export const GET = externalApi({
  auth: true,
  handler: async ({ params }) => {
    const user = await db.getUserByUuid({ uuid: params['uuid']! });
    if (!user) return new Response(null, { status: 404 });

    const fromSelf = await db.getReports({ reporter: user.username });
    const toSelf = await db.getReports({ reported: user.username });

    return new Response(
      JSON.stringify({
        from_self: fromSelf.map((report) => ({
          reported: report.reported?.uuid ?? null,
          reason: report.reason,
          created: report.createdAt?.getTime() ?? null,
          status: report.status?.status ?? null,
          url: `${BASE_PATH}/report/${report.urlHash}`
        })),
        to_self: toSelf.map((report) => ({
          reporter: report.reporter?.uuid ?? null,
          reason: report.reason,
          created: report.createdAt?.getTime() ?? null,
          status: report.status?.status ?? null,
          url: `${BASE_PATH}/report/${report.urlHash}`
        }))
      }),
      { status: 200 }
    );
  }
});
