//! Live, manual pull test (OVR-3 — the end-to-end tier).
//!
//! Ignored by default; it requires real Google OAuth credentials (run
//! `org-gdocs auth` first), network access, and a doc that has already been
//! pushed and **commented on in the Docs UI**. The offline + `mockito` tiers
//! cover the logic and request shapes; this verifies a real round-trip against
//! Google.
//!
//! Run manually, against a file that already carries a `#+GDOC_ID:` (push first)
//! and has at least one reviewer comment anchored in the Docs UI:
//!
//! ```sh
//! ORG_GDOCS_LIVE_FILE=/path/to/commented.org \
//!   cargo test -p tftio-org-gdocs --test live_pull -- --ignored --nocapture
//! ```
//!
//! It prints the merged file and the ids of any newly-filed comments; confirm a
//! correctly-sectioned `TODO` appears under the commented section.

use std::path::PathBuf;

use org_gdocs::config::{Config, EnvInputs, Paths};
use org_gdocs::google::client::GoogleClient;
use org_gdocs::{auth, pull};

#[tokio::test]
#[ignore = "requires real Google OAuth credentials, network, and a commented doc; run manually"]
async fn live_pull_merges_ui_comments() {
    // The target file is supplied out-of-band; this is a manual gate, not CI.
    #[allow(
        clippy::disallowed_methods,
        reason = "manual live-test fixture path, read once at the test edge (OVR-3)"
    )]
    let file = std::env::var_os("ORG_GDOCS_LIVE_FILE")
        .map(PathBuf::from)
        .expect("set ORG_GDOCS_LIVE_FILE to a pushed, commented org file");
    let content = std::fs::read_to_string(&file).expect("read the live fixture file");

    // Config resolution goes through the sanctioned env edge (OVR-4).
    let paths = Paths::resolve(&EnvInputs::from_env()).expect("resolve paths");
    let config = Config::load(&paths).expect("load config");

    let authenticator = auth::authenticator(&config.credentials_path, &config.token_path)
        .await
        .expect("build authenticator (run `org-gdocs auth` first)");
    let client = GoogleClient::new(authenticator).expect("build client");

    let now = chrono::Utc::now().to_rfc3339();
    let outcome = pull::pull(&client, &content, &now)
        .await
        .expect("live pull succeeds");

    println!("document: {}", outcome.document_id);
    println!("total comments: {}", outcome.total_comments);
    println!("newly filed: {:?}", outcome.new_comment_ids);
    println!("--- merged file ---\n{}", outcome.new_content);

    // The machine region is regenerated; the body still carries its keywords.
    assert!(outcome.new_content.contains("#+GDOC_LAST_PULL:"));
    assert!(outcome.new_content.contains("** Active Comments"));
}
