//! Command handlers for Aphoria CLI

use std::process::ExitCode;

use aphoria::AphoriaConfig;

use crate::cli::Commands;

mod corpus;
mod eval;
mod extractors;
mod governance;
mod lifecycle;
mod patterns;
mod policy;
mod policy_ops;
mod research;
mod scan;
mod scope;
mod shadow;
mod utils;

// Re-export for public API compatibility.
// These are used by the CLI binary but not within this module,
// so we allow unused imports for the re-export pattern.
#[allow(unused_imports)]
pub use corpus::*;
#[allow(unused_imports)]
pub use eval::*;
#[allow(unused_imports)]
pub use extractors::*;
#[allow(unused_imports)]
pub use governance::*;
#[allow(unused_imports)]
pub use lifecycle::*;
#[allow(unused_imports)]
pub use patterns::*;
#[allow(unused_imports)]
pub use policy::*;
#[allow(unused_imports)]
pub use policy_ops::*;
#[allow(unused_imports)]
pub use research::*;
#[allow(unused_imports)]
pub use scan::*;
#[allow(unused_imports)]
pub use scope::*;
#[allow(unused_imports)]
pub use shadow::*;
#[allow(unused_imports)]
pub use utils::*;

/// Dispatch and execute CLI commands
pub async fn handle_command(command: Commands, config: &AphoriaConfig) -> ExitCode {
    match command {
        Commands::Scan {
            path,
            format,
            exit_code,
            strict,
            persist,
            debug,
            sync,
            staged,
            community_preview,
            benchmark,
            show_claims,
        } => {
            if community_preview {
                scan::handle_community_preview(path, config).await
            } else {
                scan::handle_scan(
                    path,
                    format,
                    exit_code,
                    strict,
                    persist,
                    debug,
                    sync,
                    staged,
                    benchmark,
                    show_claims,
                    config,
                )
                .await
            }
        }

        Commands::Ack { command } => policy_ops::handle_ack_command(command, config).await,

        Commands::Bless { concept_path, predicate, value, reason } => {
            policy_ops::handle_bless(concept_path, predicate, value, reason, config).await
        }

        Commands::Update { concept_path, value, reason } => {
            policy_ops::handle_update(concept_path, value, reason, config).await
        }

        Commands::Baseline => policy_ops::handle_baseline(config).await,

        Commands::Diff => policy_ops::handle_diff(config).await,

        Commands::Status => policy_ops::handle_status(config).await,

        Commands::Init => policy_ops::handle_init(config).await,

        Commands::Corpus { command } => corpus::handle_corpus_command(command, config).await,

        Commands::Research { command } => research::handle_research_command(command, config).await,

        Commands::Policy { command } => policy::handle_policy_command(command, config).await,

        Commands::Extractors { command } => {
            extractors::handle_extractor_command(command, config).await
        }

        Commands::Eval { command } => eval::handle_eval_command(command, config).await,

        Commands::Patterns { command } => patterns::handle_pattern_command(command, config).await,

        Commands::Scope { command } => scope::handle_scope_command(command, config).await,

        Commands::Lifecycle { command } => {
            lifecycle::handle_lifecycle_command(command, config).await
        }

        Commands::Migrations { command } => {
            lifecycle::handle_migrations_command(command, config).await
        }

        Commands::Governance { command } => {
            governance::handle_governance_command(command, config).await
        }

        Commands::Audit { command } => governance::handle_audit_command(command, config).await,
    }
}
