//! Sync PwC Albania publications (PDFs, optional HTML) to S3 — same logic as POST /api/publications/pwc-al/sync.

use clap::Parser;
use dexter_sentinel::publications::{
    sync_pwc_al_publications, PublicationS3Service, PwcAlSyncOptions,
};

#[derive(Parser, Debug)]
#[command(name = "pwc-al-publications")]
struct Cli {
    /// List links only (no download / upload). Works without S3.
    #[arg(long, default_value_t = false)]
    dry_run: bool,

    /// Include .html publication pages in addition to PDFs.
    #[arg(long, default_value_t = false)]
    include_html: bool,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenv::dotenv().ok();
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
        )
        .init();

    let cli = Cli::parse();
    let opts = PwcAlSyncOptions {
        dry_run: cli.dry_run,
        include_html: cli.include_html,
    };

    if !opts.dry_run && PublicationS3Service::from_env().await.is_none() {
        anyhow::bail!("set S3_PUBLICATIONS_BUCKET (and AWS credentials) or use --dry-run");
    }

    let http = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(120))
        .build()?;

    let s3 = PublicationS3Service::from_env().await;
    let s3_ref = s3.as_ref();
    let out = sync_pwc_al_publications(&http, s3_ref, &opts).await?;
    println!("{}", serde_json::to_string_pretty(&out)?);
    Ok(())
}
