//! `anodizer announce` command.
//! Runs only the announce stage from a completed dist/.
//! Equivalent to GoReleaser Pro's `goreleaser announce`.

use super::helpers;
use crate::pipeline;
use anodizer_core::context::{Context, ContextOptions};
use anodizer_core::log::{StageLogger, Verbosity};
use anyhow::Result;
use std::path::PathBuf;

pub struct AnnounceOpts {
    pub dry_run: bool,
    pub dist: Option<PathBuf>,
    pub token: Option<String>,
    pub skip: Vec<String>,
    pub config_override: Option<PathBuf>,
    pub verbose: bool,
    pub debug: bool,
    pub quiet: bool,
}

pub fn run(opts: AnnounceOpts) -> Result<()> {
    let log = StageLogger::new(
        "announce",
        Verbosity::from_flags(opts.quiet, opts.verbose, opts.debug),
    );

    let mut config =
        pipeline::load_config(&pipeline::find_config(opts.config_override.as_deref())?)?;
    helpers::auto_detect_github(&mut config, &log);

    let ctx_opts = ContextOptions {
        dry_run: opts.dry_run,
        quiet: opts.quiet,
        verbose: opts.verbose,
        debug: opts.debug,
        skip_stages: opts.skip,
        token: opts.token,
        ..Default::default()
    };
    let mut ctx = Context::new(config.clone(), ctx_opts);
    helpers::setup_context(&mut ctx, &config, &log)?;

    // Load artifacts from dist/
    let dist = opts.dist.as_deref().unwrap_or(&config.dist);
    helpers::load_artifacts_from_dist(&mut ctx, dist)?;

    log.status(&format!(
        "loaded {} artifact(s) from {}",
        ctx.artifacts.all().len(),
        dist.display()
    ));

    // Run announce-only pipeline
    let p = pipeline::build_announce_pipeline();
    p.run(&mut ctx, &log)
}
