import {
  sanitizePublicMarkdown,
  summarizePublicHandoffMarkdown,
} from "./story.ts";

export {
  buildFeedStoryOutboxEntry,
  renderFeedStoryMarkdown,
  sanitizePublicMarkdown,
  summarizePublicHandoffMarkdown,
} from "./story.ts";

export function renderIssueToPrReviewerMarkdown(input: Record<string, unknown>): string {
  const title = clean(input.title) ?? clean(input.taskId) ?? "Issue-to-PR change";
  const sourceTitle = clean(input.sourceTitle);
  const sourceLocator = clean(input.sourceLocator);
  const sourceSummary = clean(input.sourceSummary);
  const handoff = summarizePublicHandoffMarkdown(input.handoffMarkdown);
  const changedFiles = Array.isArray(input.changedFiles)
    ? input.changedFiles.map((entry) => clean(entry)).filter(Boolean)
    : [];

  return [
    `# ${title}`,
    "",
    "## Source Thread",
    sourceTitle ? `- Source: ${sourceTitle}` : undefined,
    sourceLocator ? `- Locator: ${sourceLocator}` : undefined,
    "",
    "## Source Context",
    sourceSummary ?? "Source context was captured in the governed runx receipt.",
    "",
    "## Changed Files",
    changedFiles.length > 0 ? changedFiles.map((file) => `- \`${file}\``).join("\n") : "- No file list reported.",
    "",
    "## Checks",
    bullet("Governance", clean(input.governanceStatus)),
    bullet("Build", clean(input.checkStatus)),
    bullet("Passed", clean(input.buildPassed)),
    bullet("Failed", clean(input.buildFailed)),
    bullet("Review", clean(input.reviewVerdict)),
    bullet("Blocking findings", clean(input.blockingCount)),
    bullet("Non-blocking findings", clean(input.nonBlockingCount)),
    clean(input.qualityGateSummary) ? `- Quality gate: ${clean(input.qualityGateSummary)}` : undefined,
    "",
    "## scafld Handoff",
    handoff ?? "No public handoff summary was available.",
    "",
    "## Human Merge Gate",
    "Review the diff and merge only after the generated change is acceptable. runx does not auto-merge generated PRs.",
  ].filter((line) => line !== undefined).join("\n");
}

function clean(value: unknown): string | undefined {
  const sanitized = sanitizePublicMarkdown(value)?.trim();
  return sanitized || undefined;
}

function bullet(label: string, value: string | undefined): string | undefined {
  return value ? `- ${label}: ${value}` : undefined;
}
