/**
 * pipeline/report-title.ts
 *
 * Pure function that generates descriptive report titles from provenance
 * metadata. The title is the primary display string shown in dashboards,
 * Slack digests, and Studio views — it conveys trigger context, evaluated
 * areas, source/perspective, and document scope at a glance.
 *
 * Score is intentionally omitted from the title since it is surfaced
 * heavily elsewhere in the UI. The `tag` field (on Report) is preserved
 * as a secondary label; the title is the primary display string.
 *
 * Segments are joined with ` · ` (middle dot with spaces).
 *
 * @see docs/design-docs/report-store/domain-model.md
 * @see packages/eval/src/pipeline/provenance.ts — builds the provenance input
 */
// ---------------------------------------------------------------------------
// Segment builders
// ---------------------------------------------------------------------------
const SEPARATOR = " · ";
/** Segment 1 — human-readable trigger context */
function triggerSegment(trigger) {
    switch (trigger.type) {
        case "scheduled": {
            const name = trigger.schedule.replace(/-/g, " ");
            return name.charAt(0).toUpperCase() + name.slice(1);
        }
        case "ci":
            return "CI eval";
        case "webhook":
            return "Content change";
        case "cross-repo": {
            // Only show the repo name if callerRepo looks like "owner/repo".
            // Numeric IDs (e.g. GITHUB_REPOSITORY_OWNER_ID fallback) are not useful.
            const repo = trigger.callerRepo;
            if (repo.includes("/")) {
                const shortName = repo.split("/").pop() ?? repo;
                return `Cross-repo (${shortName})`;
            }
            return "Cross-repo";
        }
        case "manual":
            return "Manual eval";
    }
}
/** Segment 2 — areas evaluated (omitted when empty) */
function areasSegment(areas, totalAreaCount) {
    if (areas.length === 0)
        return undefined;
    if (areas.length <= 3) {
        return areas.join(", ");
    }
    if (totalAreaCount !== undefined && areas.length === totalAreaCount) {
        return "All areas";
    }
    return `${areas.length} areas`;
}
/** Segment 3 — source context (omitted when default production, no perspective) */
function sourceSegment(source) {
    const parts = [];
    if (source.perspective) {
        parts.push(`perspective: ${source.perspective}`);
    }
    if (source.name !== "production") {
        parts.push(source.name);
    }
    return parts.length > 0 ? parts.join(", ") : undefined;
}
/** Segment 4 — target documents (omitted when not scoped) */
function targetDocumentsSegment(targetDocuments) {
    if (!targetDocuments || targetDocuments.length === 0)
        return undefined;
    if (targetDocuments.length === 1) {
        return targetDocuments[0];
    }
    return `${targetDocuments.length} documents`;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Generate a descriptive report title from provenance metadata.
 *
 * The title is composed of up to four segments separated by ` · `:
 *
 * 1. **Trigger context** — what initiated the evaluation (always present)
 * 2. **Areas** — which feature areas were evaluated (omitted if empty)
 * 3. **Source context** — non-default source or perspective (omitted if default)
 * 4. **Target documents** — scoped document IDs (omitted if not scoped)
 *
 * @example
 * ```ts
 * generateReportTitle({
 *   provenance: {
 *     areas: ["GROQ", "Mutations"],
 *     mode: "baseline",
 *     source: { name: "production" },
 *     trigger: { type: "manual" },
 *   },
 * })
 * // → "Manual eval · GROQ, Mutations"
 * ```
 */
export function generateReportTitle(input) {
    const { provenance, totalAreaCount } = input;
    const segments = [triggerSegment(provenance.trigger)];
    const areas = areasSegment(provenance.areas, totalAreaCount);
    if (areas)
        segments.push(areas);
    const source = sourceSegment(provenance.source);
    if (source)
        segments.push(source);
    const docs = targetDocumentsSegment(provenance.targetDocuments);
    if (docs)
        segments.push(docs);
    return segments.join(SEPARATOR);
}
