// SPDX-License-Identifier: Apache-2.0

use crate::MdbClient;

use std::path::PathBuf;
use std::process::ExitCode;

use anyhow::Result;
use clap::{Parser, ValueHint};

/// See if similar samples to a given sample are known to the server
#[derive(Parser, Clone, Debug, PartialEq)]
pub struct Similar {
    /// The file to send
    #[arg(value_hint = ValueHint::FilePath)]
    file: PathBuf,
}

impl Similar {
    pub async fn exec(&self, config: &MdbClient) -> Result<ExitCode> {
        let contents = std::fs::read(&self.file)?;
        let res = config.similar(&contents).await?;

        if res.results.is_empty() {
            println!("No results.");
        } else {
            println!("Found {} similar files:", res.results.len());
            for result in res.results {
                println!("SHA-256: {}", result.sha256);
                for (sim_type, sim_score) in &result.algorithms {
                    println!("\t{sim_type}, {sim_score:04}");
                }
            }
        }

        Ok(ExitCode::SUCCESS)
    }
}

#[test]
fn verify_cli() {
    use clap::CommandFactory;

    Similar::command().debug_assert();
}
