use std::path::PathBuf;

use hen::{automation, error::HenResult};

use crate::{
    cli::{CommandOutcome, VerifyArgs},
    cli_output::{print_machine_error, print_verification_report, print_verification_result},
};

pub(crate) fn verify(args: VerifyArgs) -> HenResult<CommandOutcome> {
    if args.output.is_text() {
        verify_text(args)?;
        return Ok(CommandOutcome::success());
    }

    Ok(verify_machine_output(args))
}

fn verify_text(args: VerifyArgs) -> HenResult<()> {
    let result = automation::verify_path(PathBuf::from(args.path))?;
    print_verification_result(&result);
    Ok(())
}

fn verify_machine_output(args: VerifyArgs) -> CommandOutcome {
    let output = args.output;
    let suite_name = args.path.clone();

    match automation::verify_path(PathBuf::from(args.path)) {
        Ok(result) => {
            print_verification_report(output, &result);
            CommandOutcome::success()
        }
        Err(err) => {
            print_machine_error(output, &suite_name, "verify", &err);
            CommandOutcome::with_exit_code(err.exit_code())
        }
    }
}