#!/usr/bin/env bash
set -euo pipefail

ARTICLE_FILE="${1:?usage: article-research <path-to-article.md> <output-file>}"
OUTPUT_FILE="${2:?usage: article-research <path-to-article.md> <output-file>}"

if [[ ! -f "$ARTICLE_FILE" ]]; then
    echo "Error: File not found: $ARTICLE_FILE" >&2
    exit 1
fi

# Ensure output directory exists
mkdir -p "$(dirname "$OUTPUT_FILE")"

ARTICLE=$(< "$ARTICLE_FILE")

exec codex exec --skip-git-repo-check -c 'web_search="live"' "You are a fact-checking research assistant for BRIX Templates blog posts. Your job is to verify EVERY factual claim in the article below against current, real-world sources. This applies to both Webflow and Framer articles.

OUTPUT FILE: ${OUTPUT_FILE}
You MUST write your findings to this file as you go. Every time you complete a finding or verification, IMMEDIATELY append it to the file. Do not accumulate results in memory — write them incrementally so progress is never lost.

--- ARTICLE START ---
${ARTICLE}
--- ARTICLE END ---

Instructions:

1) Start by writing a header to the output file:
   # Article Research Findings
   **Article:** [article title from the content above]
   **Researched:** [today's date]
   ---

2) Read the entire article above carefully. Identify EVERY factual claim that could have changed since the article was written or last updated. This includes but is not limited to: prices, plan names, plan tiers, feature limits, included items, bandwidth caps, CMS item limits, page limits, storage limits, seat counts, editor permissions, deprecation dates, migration deadlines, UI descriptions, dashboard locations, API endpoints, integration details, and any other concrete facts.

3) For EACH claim you identify, search the web extensively. You MUST run dozens of searches — not 2 or 3. Each claim is its own research thread. Try different search angles:
   - Product name + specific feature (e.g. 'Webflow CMS plan bandwidth limit')
   - Product name + pricing + year (e.g. 'Webflow pricing 2026')
   - Product name + changelog or updates (e.g. 'Webflow pricing changes 2026')
   - Product name + specific plan name (e.g. 'Webflow Business plan features')
   - Product name + comparison terms (e.g. 'Webflow plans comparison')
   Do NOT stop after one search per claim. Do NOT settle on the first result you find. Keep searching until you have high confidence.

4) When you find a relevant page URL, use web_fetch to read the FULL content of that page. This is critical. Search snippets are often truncated, outdated, or missing key details. You MUST fetch and read:
   - The official pricing page (e.g. webflow.com/pricing, framer.com/pricing)
   - The official help center / documentation pages
   - Changelog or release notes pages
   - Any other official source that contains the specific data you need
   If a search result looks promising, ALWAYS fetch it. Never skip a page to save time.

5) Cross-reference every finding across at least 2 sources when possible. Priority: official source pages > official help docs > official blog posts > third-party blogs > forums.

6) For each finding where the article's information is outdated or wrong, IMMEDIATELY APPEND to the output file in this EXACT format:

## Finding N: [Short description]

**Location in article:** [Section name / paragraph / list item — be specific enough to locate it]
**Old value:** [What the article currently says — quote it exactly]
**New value:** [What it should say now — write the corrected text]
**Confidence:** High | Medium | Low
**Source(s):**
- [URL 1] (fetched and verified)
- [URL 2] (fetched and verified)

**Notes:** [Any additional context, caveats, or things the user should manually verify]

7) Also check for claims that ARE still correct. For each verified-as-correct claim, IMMEDIATELY APPEND to the output file:

## Verified N: [Short description]

**Location in article:** [Section]
**Current value:** [What the article says]
**Status:** Still correct as of [date]
**Source:** [URL you verified against]

8) After ALL claims have been researched, append a summary to the output file:

## Summary

- **Total claims checked:** N
- **Findings (outdated/wrong):** N
- **High confidence findings:** N
- **Medium confidence findings:** N (user should verify)
- **Low confidence findings:** N (user must verify)
- **Verified as correct:** N

9) Finally, append a Sources section listing EVERY URL you actually fetched and read (not just searched):

## Sources

- [URL 1] — [what information you got from it]
- [URL 2] — [what information you got from it]
- ...

CRITICAL RULES:
- WRITE TO THE OUTPUT FILE AS YOU GO. After each finding or verification, immediately append it to ${OUTPUT_FILE}. Do not wait until the end.
- Run DOZENS of searches. If you run fewer than 15 searches total, you are not being thorough enough.
- ALWAYS web_fetch official source pages. Never rely only on search snippets.
- When showing prices, note both billing cadences if available (yearly and monthly).
- If you cannot verify a claim with confidence, mark it as Low confidence and explain what you tried.
- Be exhaustive. A single missed price change or wrong feature limit makes the output unreliable.
- Frame everything as 'updates' — never say 'errors,' 'mistakes,' or 'corrections.'
- You MAY use shell commands ONLY to write/append to the output file (e.g. cat, echo, tee). Do not use shell commands for anything else."
