/**
 * POST /api/admin/market-position/seed
 *
 * Triggers a full corpus seed: discovers all (vertical, locale) cells
 * from Brand rows, derives probe queries from AI attribution fragments,
 * and upserts them into QueryCorpusEntry. Idempotent -- safe to re-run.
 *
 * Superadmin-only.
 */

import { NextResponse } from "next/server";
import { requireSuperadmin } from "@/lib/admin-auth";
import { seedAllCells } from "@/lib/market-position/seed-corpus";

export const dynamic = "force-dynamic";

export async function POST(req: Request): Promise<Response> {
  const denied = await requireSuperadmin();
  if (denied) return denied;

  try {
    const result = await seedAllCells();
    return NextResponse.json({
      ok: true,
      cells: result.cells,
      queries: result.queries,
    });
  } catch (err) {
    console.error("[market-position/seed] error", err);
    return NextResponse.json(
      { error: "Seed failed", detail: err instanceof Error ? err.message : String(err) },
      { status: 500 },
    );
  }
}
