use std::process::ExitCode;

use anyhow::Result;

use crate::handoff;
use crate::output::{self, OutputFormat};
use crate::paths;
use crate::recovery;

pub(crate) fn handoff(command: crate::HandoffCommand, format: OutputFormat) -> Result<ExitCode> {
    match command {
        crate::HandoffCommand::Refresh(args) => {
            let repo_path = paths::cli::resolve(&args.path)?;
            let report = handoff::refresh::run(&repo_path, args.profile.as_deref(), args.write)?;
            output::render_report(format, &report)
        }
        crate::HandoffCommand::Export(args) => {
            let repo_path = paths::cli::resolve(&args.path)?;
            let report = handoff::export::run(&repo_path, args.profile.as_deref())?;
            output::render_report(format, &report)
        }
        crate::HandoffCommand::Write(args) => {
            let repo_path = paths::cli::resolve(&args.path)?;
            let report = handoff::write::run(
                &repo_path,
                args.profile.as_deref(),
                args.protected_write.into_state_options(),
                handoff::write::WriteOptions {
                    allow_missing_refs: args.allow_missing_refs,
                },
            )?;
            output::render_report(format, &report)
        }
    }
}

pub(crate) fn recovery(command: crate::RecoveryCommand, format: OutputFormat) -> Result<ExitCode> {
    match command {
        crate::RecoveryCommand::Write(args) => {
            let repo_path = paths::cli::resolve(&args.path)?;
            let report = recovery::write(
                &repo_path,
                args.profile.as_deref(),
                args.protected_write.into_state_options(),
            )?;
            output::render_report(format, &report)
        }
    }
}
