//! Enrich command handler.
//!
//! Enriches article URLs with GKG metadata and optionally fetches full text.

use crate::api::client::GdeltClient;
use crate::cli::args::{EnrichArgs, GlobalArgs};
use crate::cli::output::OutputWriter;
use crate::enrich::{ArticleInput, EnrichmentService};
use crate::error::{ExitStatus, Result};
use std::io::{self, BufRead};
use std::time::Duration;

/// Handle enrich command
pub async fn handle_enrich(args: EnrichArgs, global: &GlobalArgs) -> Result<ExitStatus> {
    let output = OutputWriter::new(global);

    // Collect URLs from arguments or stdin
    let urls: Vec<String> = if args.stdin {
        let stdin = io::stdin();
        stdin
            .lock()
            .lines()
            .filter_map(|line| line.ok())
            .map(|line| line.trim().to_string())
            .filter(|line| !line.is_empty() && line.starts_with("http"))
            .collect()
    } else {
        args.urls
    };

    if urls.is_empty() {
        eprintln!("No URLs provided.");
        eprintln!();
        eprintln!("Usage:");
        eprintln!("  gdelt enrich https://example.com/article");
        eprintln!("  gdelt enrich --stdin < urls.txt");
        return Ok(ExitStatus::ValidationError);
    }

    if !global.quiet {
        eprintln!("Enriching {} article(s)...", urls.len());
    }

    // Create enrichment service
    let service = if args.fetch_text {
        let client = GdeltClient::with_timeout(Duration::from_secs(global.timeout))?;
        EnrichmentService::with_fetcher(client)?
    } else {
        EnrichmentService::new()?
    };

    // Convert to ArticleInput
    let articles: Vec<ArticleInput> = urls
        .into_iter()
        .map(ArticleInput::new)
        .collect();

    // Enrich articles
    let (enriched, stats) = service
        .enrich_batch(articles, args.fetch_text, args.concurrency)
        .await;

    // Output results
    let result = serde_json::json!({
        "articles": enriched,
        "stats": stats,
    });

    output.write_value(&result)?;

    if !global.quiet {
        eprintln!();
        eprintln!("Enrichment complete:");
        eprintln!("  GKG found: {}/{}", stats.gkg_found, stats.total);
        if args.fetch_text {
            eprintln!("  Text fetched: {}", stats.text_fetched);
        }
    }

    Ok(ExitStatus::Success)
}
