use crate::client::KeepClient;
use crate::modes::common::{
    DisplayItemInfo, OutputFormat, format_size, render_item_info_table, resolve_item_ids,
    settings_output_format,
};
use clap::Command;
use log::debug;

pub fn mode(
    client: &KeepClient,
    _cmd: &mut Command,
    settings: &crate::config::Settings,
    ids: &[i64],
    tags: &[String],
) -> Result<(), anyhow::Error> {
    debug!("CLIENT_INFO: Getting item info via remote server");

    let output_format = settings_output_format(settings);
    let item_ids = resolve_item_ids(client, ids, tags)?;

    for &id in &item_ids {
        let item = client.get_item_info(id)?;

        match output_format {
            OutputFormat::Json | OutputFormat::Yaml => {
                crate::modes::common::print_serialized(&item, &output_format)?;
            }
            OutputFormat::Table => {
                let display = DisplayItemInfo {
                    id: item.id,
                    timestamp: item.ts.clone(),
                    path: String::new(),
                    stream_size: item
                        .size
                        .map(|s| format_size(s as u64, settings.human_readable))
                        .unwrap_or_else(|| "N/A".to_string()),
                    compression: item.compression.clone(),
                    file_size: String::new(),
                    tags: item.tags.clone(),
                    metadata: item
                        .metadata
                        .iter()
                        .map(|(k, v)| (k.clone(), v.clone()))
                        .collect(),
                };
                render_item_info_table(&display, &settings.table_config);
            }
        }
    }

    Ok(())
}
