//! Tauri commands for unit conversions and formatting
//!
//! TRACES: UR-005 | DR-009
//!
//! These commands expose conversion utilities to the frontend,
//! allowing centralized conversion logic in Rust.

use crate::utils::conversions::{
    calculate_progress, format_time, format_time_long, percent_to_volume, ticks_to_seconds,
};

/// Format time in seconds to MM:SS display string
///
/// # Arguments
/// * `seconds` - Time in seconds
///
/// # Returns
/// Formatted string like "3:45" or "12:09"
#[tauri::command]
#[specta::specta]
pub fn format_time_seconds(seconds: f64) -> String {
    format_time(seconds)
}

/// Format time in seconds to HH:MM:SS or MM:SS display string
///
/// Automatically chooses format based on duration:
/// - Less than 1 hour: Returns MM:SS format
/// - 1 hour or more: Returns HH:MM:SS format
///
/// # Arguments
/// * `seconds` - Time in seconds
///
/// # Returns
/// Formatted string like "1:23:45" or "3:45"
#[tauri::command]
#[specta::specta]
pub fn format_time_seconds_long(seconds: f64) -> String {
    format_time_long(seconds)
}

/// Convert Jellyfin ticks to seconds
///
/// # Arguments
/// * `ticks` - Time in Jellyfin ticks (10,000,000 ticks = 1 second)
///
/// # Returns
/// Time in seconds
#[tauri::command]
#[specta::specta]
pub fn convert_ticks_to_seconds(ticks: i64) -> f64 {
    ticks_to_seconds(ticks)
}

/// Calculate progress percentage from position and duration
///
/// # Arguments
/// * `position` - Current position in seconds
/// * `duration` - Total duration in seconds
///
/// # Returns
/// Progress as percentage (0.0 to 100.0)
#[tauri::command]
#[specta::specta]
pub fn calc_progress(position: f64, duration: f64) -> f64 {
    calculate_progress(position, duration)
}

/// Convert percentage volume (0-100) to normalized (0.0-1.0)
///
/// # Arguments
/// * `percent` - Volume as percentage (0 to 100)
///
/// # Returns
/// Normalized volume (0.0 to 1.0)
#[tauri::command]
#[specta::specta]
pub fn convert_percent_to_volume(percent: f64) -> f64 {
    percent_to_volume(percent)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_time_seconds() {
        assert_eq!(format_time_seconds(0.0), "0:00");
        assert_eq!(format_time_seconds(59.0), "0:59");
        assert_eq!(format_time_seconds(60.0), "1:00");
        assert_eq!(format_time_seconds(125.0), "2:05");
        assert_eq!(format_time_seconds(3661.0), "61:01");
    }

    #[test]
    fn test_format_time_seconds_long() {
        assert_eq!(format_time_seconds_long(0.0), "0:00");
        assert_eq!(format_time_seconds_long(59.0), "0:59");
        assert_eq!(format_time_seconds_long(3599.0), "59:59");
        assert_eq!(format_time_seconds_long(3600.0), "1:00:00");
        assert_eq!(format_time_seconds_long(3661.0), "1:01:01");
        assert_eq!(format_time_seconds_long(7384.0), "2:03:04");
    }

    #[test]
    fn test_convert_ticks_to_seconds() {
        assert_eq!(convert_ticks_to_seconds(0), 0.0);
        assert_eq!(convert_ticks_to_seconds(10_000_000), 1.0);
        assert_eq!(convert_ticks_to_seconds(5_000_000), 0.5);
        assert_eq!(convert_ticks_to_seconds(60_000_000), 6.0);
    }

    #[test]
    fn test_calc_progress() {
        assert_eq!(calc_progress(0.0, 100.0), 0.0);
        assert_eq!(calc_progress(50.0, 100.0), 50.0);
        assert_eq!(calc_progress(100.0, 100.0), 100.0);
        assert_eq!(calc_progress(25.0, 0.0), 0.0); // Invalid duration
        assert_eq!(calc_progress(150.0, 100.0), 100.0); // Clamped
    }

    #[test]
    fn test_convert_percent_to_volume() {
        assert_eq!(convert_percent_to_volume(0.0), 0.0);
        assert_eq!(convert_percent_to_volume(50.0), 0.5);
        assert_eq!(convert_percent_to_volume(100.0), 1.0);
        assert_eq!(convert_percent_to_volume(150.0), 1.0); // Clamped
        assert_eq!(convert_percent_to_volume(-10.0), 0.0); // Clamped
    }
}
