use crate::config;
use crate::modes::common::{DisplayItemInfo, OutputFormat, format_size, render_item_info_table};
use crate::services::types::ItemWithMeta;
use anyhow::{Context, Result, anyhow};
use clap::Command;
use clap::error::ErrorKind;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use crate::services::item_service::ItemService;
use chrono::prelude::*;

/// Displays detailed information about an item or the last item if no ID/tags specified.
///
/// Supports table, JSON, or YAML output formats. Validates input (at most one ID, no mixing IDs/tags).
/// Uses ItemService to fetch the item and displays via helpers.
///
/// # Arguments
///
/// * `cmd` - Mutable Clap command for error handling and exiting on invalid args.
/// * `settings` - Application settings for output formatting and human-readable sizes.
/// * `ids` - Mutable vector of item IDs (at most one; cleared if tags used).
/// * `tags` - Mutable vector of tags (mutually exclusive with IDs).
/// * `conn` - Mutable database connection for querying items.
/// * `data_path` - Path to data directory for file metadata.
///
/// # Returns
///
/// `Ok(())` on success, or `Err(anyhow::Error)` if item not found or DB query fails.
///
/// # Errors
///
/// * Clap errors if invalid args (e.g., multiple IDs).
/// * Anyhow error if no matching item found.
///
/// # Examples
///
/// ```ignore
/// // Example usage requires Command, Settings, Connection, and PathBuf instances
/// mode_info(&mut cmd, &settings, &mut vec![123], &mut vec![], &mut conn, data_path)?;
/// ```
pub fn mode_info(
    cmd: &mut Command,
    settings: &config::Settings,
    ids: &mut [i64],
    tags: &mut [String],
    conn: &mut rusqlite::Connection,
    data_path: PathBuf,
) -> Result<()> {
    // For --info, we can use either IDs or tags, but not both
    if !ids.is_empty() && !tags.is_empty() {
        cmd.error(
            ErrorKind::InvalidValue,
            "Both ID and tags given, you must supply either IDs or tags when using --info",
        )
        .exit();
    } else if ids.len() > 1 {
        cmd.error(
            ErrorKind::InvalidValue,
            "More than one ID given, you must supply exactly one ID when using --info",
        )
        .exit();
    }
    // If both are empty, find_item will find the last item

    let item_service = ItemService::new(data_path.clone());
    let meta_filter: std::collections::HashMap<String, Option<String>> = settings
        .meta
        .iter()
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect();
    let item_with_meta = item_service
        .find_item(conn, ids, tags, &meta_filter)
        .map_err(|e| anyhow!("Unable to find matching item in database: {}", e))?;

    show_item(item_with_meta, settings, data_path)
}

#[derive(Debug, Serialize, Deserialize)]
/// Structured representation of item information for JSON/YAML output.
///
/// This struct serializes item details including ID, timestamp, sizes, compression, tags, and metadata
/// for non-table output formats.
///
/// # Fields
///
/// * `id` - The unique item ID.
/// * `timestamp` - Formatted timestamp string.
/// * `path` - Full file path to the item.
/// * `stream_size` - Original uncompressed size in bytes (optional).
/// * `stream_size_formatted` - Human-readable stream size.
/// * `compression` - Compression type used.
/// * `file_size` - Compressed file size in bytes (optional).
/// * `file_size_formatted` - Human-readable file size.
/// * `tags` - List of associated tags.
/// * `meta` - Metadata key-value pairs.
pub struct ItemInfo {
    id: i64,
    timestamp: String,
    path: String,
    stream_size: Option<u64>,
    stream_size_formatted: String,
    compression: String,
    file_size: Option<u64>,
    file_size_formatted: String,
    tags: Vec<String>,
    meta: std::collections::HashMap<String, String>,
}

/// Displays item information in table format or delegates to structured output.
///
/// Builds a comfy-table for tabular display or calls structured helper for JSON/YAML.
/// Handles file size via metadata and formats tags/meta accordingly.
///
/// # Arguments
///
/// * `item_with_meta` - Item with associated metadata and tags.
/// * `settings` - Application settings for formatting (e.g., human-readable sizes).
/// * `data_path` - Path to data directory for calculating compressed file size.
///
/// # Returns
///
/// `Ok(())` on success, or `Err(anyhow::Error)` if path resolution fails.
///
/// # Errors
///
/// * Anyhow error if item path cannot be stringified.
///
/// # Examples
///
/// ```ignore
/// // Example usage requires ItemWithMeta, Settings, and PathBuf instances
/// show_item(item_with_meta, &settings, data_path)?;
/// ```
fn show_item(
    item_with_meta: ItemWithMeta,
    settings: &config::Settings,
    data_path: PathBuf,
) -> Result<()> {
    let output_format = crate::modes::common::settings_output_format(settings);

    if output_format != OutputFormat::Table {
        return show_item_structured(item_with_meta, settings, data_path, output_format);
    }

    let item = item_with_meta.item;
    let item_id = item.id.context("Item missing ID")?;
    let item_tags: Vec<String> = item_with_meta.tags.iter().map(|t| t.name.clone()).collect();

    let mut item_path_buf = data_path.clone();
    item_path_buf.push(item_id.to_string());

    let size_str = match item.size {
        Some(size) => format_size(size as u64, settings.human_readable),
        None => "Missing".to_string(),
    };

    let file_size_str = match item_path_buf.metadata() {
        Ok(metadata) => format_size(metadata.len(), settings.human_readable),
        Err(_) => "Missing".to_string(),
    };

    let metadata: Vec<(String, String)> = item_with_meta
        .meta
        .iter()
        .map(|m| (m.name.clone(), m.value.clone()))
        .collect();

    let display = DisplayItemInfo {
        id: item_id,
        timestamp: item.ts.with_timezone(&Local).format("%F %T %Z").to_string(),
        path: item_path_buf
            .to_str()
            .ok_or_else(|| anyhow::anyhow!("non-UTF-8 item path"))?
            .to_string(),
        stream_size: size_str,
        compression: item.compression.clone(),
        file_size: file_size_str,
        tags: item_tags,
        metadata,
    };

    render_item_info_table(&display, &settings.table_config);
    Ok(())
}

/// Displays item information in structured JSON or YAML format.
///
/// Serializes ItemInfo and prints pretty-formatted output. Handles file metadata for sizes.
///
/// # Arguments
///
/// * `item_with_meta` - Item with metadata and tags.
/// * `settings` - Settings for size formatting (human-readable).
/// * `data_path` - Data path for compressed file size calculation.
/// * `output_format` - JSON or YAML (Table is unreachable here).
///
/// # Returns
///
/// `Ok(())` on success, or `Err(anyhow::Error)` if serialization or path fails.
///
/// # Errors
///
/// * Serde errors during JSON/YAML serialization.
/// * Anyhow error if file metadata unavailable.
///
/// # Examples
///
/// ```ignore
/// // Example usage requires ItemWithMeta, Settings, PathBuf, and OutputFormat instances
/// show_item_structured(item_with_meta, &settings, data_path, OutputFormat::Json)?;
/// ```
fn show_item_structured(
    item_with_meta: ItemWithMeta,
    settings: &config::Settings,
    data_path: PathBuf,
    output_format: OutputFormat,
) -> Result<()> {
    let item_tags: Vec<String> = item_with_meta.tags.iter().map(|t| t.name.clone()).collect();
    let meta_map = item_with_meta.meta_as_map();
    let item = item_with_meta.item;
    let item_id = item.id.context("Item missing ID")?;

    let mut item_path_buf = data_path.clone();
    item_path_buf.push(item_id.to_string());

    let file_size = item_path_buf.metadata().map(|m| m.len()).ok();
    let file_size_formatted = match file_size {
        Some(size) => format_size(size, settings.human_readable),
        None => "Missing".to_string(),
    };

    let stream_size_formatted = match item.size {
        Some(size) => format_size(size as u64, settings.human_readable),
        None => "Missing".to_string(),
    };

    let item_info = ItemInfo {
        id: item_id,
        timestamp: item
            .ts
            .with_timezone(&chrono::Local)
            .format("%F %T %Z")
            .to_string(),
        path: item_path_buf.to_str().unwrap_or("").to_string(),
        stream_size: item.size.map(|s| s as u64),
        stream_size_formatted,
        compression: item.compression,
        file_size,
        file_size_formatted,
        tags: item_tags,
        meta: meta_map,
    };

    crate::modes::common::print_serialized(&item_info, &output_format)?;

    Ok(())
}
