import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { resolveLoc } from "@/lib/loc";

// Public: list all projects (served from our DB), each with a resolved
// line-of-code count (manual `loc` or fetched from `locEndpoint`, cached).
export async function GET() {
  // Ordered by size (Big -> MediumSized -> Small via the enum's declared order),
  // then alphabetically by name.
  const projects = await prisma.project.findMany({
    orderBy: [{ size: "asc" }, { name: "asc" }],
  });

  const withLoc = await Promise.all(
    projects.map(async (p) => ({
      id: p.id,
      name: p.name,
      label: p.label,
      size: p.size,
      imageUrl: p.imageUrl,
      link: p.link,
      linkIsDemo: p.linkIsDemo,
      sourceCode: p.sourceCode,
      description: p.description,
      why: p.why,
      note: p.note,
      tags: p.tags,
      loc: p.loc,
      resolvedLoc: await resolveLoc(p.loc, p.locEndpoint),
      createdAt: p.createdAt,
      updatedAt: p.updatedAt,
    })),
  );

  return NextResponse.json(withLoc);
}
