use std::process::ExitCode;

use anyhow::Result;

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

pub(crate) fn attach(args: crate::AttachArgs, format: OutputFormat) -> Result<ExitCode> {
    let repo_path = paths::cli::resolve(&args.path)?;
    let report = commands::attach::run(
        &repo_path,
        args.profile.as_deref(),
        args.project_id.as_deref(),
        args.display_name.as_deref(),
    )?;
    output::render_report(format, &report)
}

pub(crate) fn init(args: crate::InitArgs, format: OutputFormat) -> Result<ExitCode> {
    let repo_path = paths::cli::resolve(&args.path)?;
    let report = commands::host::init(
        &repo_path,
        args.profile.as_deref(),
        args.project_id.as_deref(),
        args.display_name.as_deref(),
        args.force,
    )?;
    output::render_report(format, &report)
}

pub(crate) fn scaffold(args: crate::ScaffoldArgs, format: OutputFormat) -> Result<ExitCode> {
    let repo_path = paths::cli::resolve(&args.path)?;
    let report = commands::scaffold::run(&repo_path, args.force)?;
    output::render_report(format, &report)
}

pub(crate) fn link(args: crate::LinkArgs, format: OutputFormat) -> Result<ExitCode> {
    let repo_path = paths::cli::resolve(&args.path)?;
    let report = commands::link::run(
        &repo_path,
        args.profile.as_deref(),
        args.project_id.as_deref(),
        args.display_name.as_deref(),
    )?;
    output::render_report(format, &report)
}

pub(crate) fn unlink(args: crate::UnlinkArgs, format: OutputFormat) -> Result<ExitCode> {
    let repo_path = paths::cli::resolve(&args.path)?;
    let report = commands::unlink::run(&repo_path)?;
    output::render_report(format, &report)
}
