/**
 * GET /api/profile/me/approved-domains
 *
 * Liste der vom User submitten und vom Admin genehmigten Domains
 * (= Community-Beitrag-Benchmark, siehe project_profile_page_design.md §2).
 *
 * Source: domain_submissions WHERE userId = me AND status = 'approved'
 * (NICHT user_custom_domains — domain_submissions ist source of truth für
 * "von dir submitted und approved").
 *
 * Response shape:
 *   { count: number, list: Array<{ domain, approvedAt }> }
 *
 * Sortiert: approvedAt DESC. Cap 100 (UI rendert max 100, mehr braucht
 * Pagination — kann später additiv kommen).
 */
import { requireUser } from "../../../utils/auth";
import { usePrisma } from "../../../utils/prisma";

const MAX_LIST_ITEMS = 100;

export default defineEventHandler(async (event) => {
  const user = await requireUser(event);
  const db = usePrisma();

  const [count, rows] = await Promise.all([
    db.domainSubmission.count({
      where: { userId: user.id, status: "approved" },
    }),
    db.domainSubmission.findMany({
      where: { userId: user.id, status: "approved" },
      orderBy: { reviewedAt: "desc" },
      take: MAX_LIST_ITEMS,
      select: { domain: true, reviewedAt: true },
    }),
  ]);

  return {
    success: true,
    data: {
      count,
      list: rows.map((r) => ({
        domain: r.domain,
        approvedAt: r.reviewedAt?.toISOString() ?? null,
      })),
    },
  };
});
