import { NextResponse } from "next/server";
import { resolveRequestTenant } from "@/core/tenant/request-tenant";
import { listPublishedProjects } from "@/services/project-service";

/**
 * Public tenant projects API.
 * Tenant comes from trusted hostname context — NOT from query tenant_id.
 */
export async function GET() {
  const resolved = await resolveRequestTenant();
  if (!resolved.ok) {
    const status = resolved.code === "SUSPENDED" ? 403 : 404;
    return NextResponse.json({ error: "Tenant not available" }, { status });
  }

  const projects = await listPublishedProjects(resolved.context);

  return NextResponse.json({
    tenant: {
      slug: resolved.context.tenantSlug,
      studioName: resolved.context.studioName,
    },
    projects: projects.map((project) => ({
      id: project.id,
      title: project.title,
      slug: project.slug,
      location: project.location,
      year: project.year,
      publishedAt: project.publishedAt,
    })),
  });
}
