use std::process::ExitCode;

use anyhow::Result;

use crate::commands;
use crate::output::{self, OutputFormat};
use crate::paths;

pub(crate) fn dispatch(command: crate::RepoCommand, format: OutputFormat) -> Result<ExitCode> {
    match command {
        crate::RepoCommand::Status(args) => {
            let repo_path = paths::cli::resolve(&args.path)?;
            let report = commands::repo::status(&repo_path, args.profile.as_deref())?;
            output::render_report(format, &report)
        }
        crate::RepoCommand::List(args) => {
            let repo_path = paths::cli::resolve(&args.path)?;
            let report = commands::repo::list(&repo_path, args.profile.as_deref())?;
            output::render_report(format, &report)
        }
        crate::RepoCommand::Relink(args) => {
            let repo_path = paths::cli::resolve(&args.path)?;
            let report =
                commands::repo::relink(&repo_path, args.profile.as_deref(), &args.locality_id)?;
            output::render_report(format, &report)
        }
        crate::RepoCommand::Merge(args) => {
            let repo_path = paths::cli::resolve(&args.path)?;
            let report = commands::repo::merge(
                &repo_path,
                args.profile.as_deref(),
                &args.target_locality_id,
                &args.source_locality_id,
                args.force,
            )?;
            output::render_report(format, &report)
        }
        crate::RepoCommand::Split(args) => {
            let repo_path = paths::cli::resolve(&args.path)?;
            let report = commands::repo::split(
                &repo_path,
                args.profile.as_deref(),
                &args.source_locality_id,
            )?;
            output::render_report(format, &report)
        }
    }
}
