//! TRACES: UR-010 | JA-021 | DR-037

use crate::jellyfin::client::SessionInfo;
use crate::session_poller::{PollingHint, SessionPollerManager};
use std::sync::Arc;
use tauri::State;

/// Tauri state wrapper for SessionPollerManager
pub struct SessionPollerWrapper(pub Arc<SessionPollerManager>);

/// Set polling frequency hint based on UI state
#[tauri::command]
#[specta::specta]
pub fn sessions_set_polling_hint(
    poller: State<'_, SessionPollerWrapper>,
    hint: String,
) -> Result<(), String> {
    let parsed_hint = match hint.as_str() {
        "cast_active" => PollingHint::CastActive,
        "cast_discovery" => PollingHint::CastDiscovery,
        "normal" => PollingHint::Normal,
        _ => return Err(format!("Invalid polling hint: {}", hint)),
    };

    poller.0.set_polling_hint(parsed_hint);
    Ok(())
}

/// Manually trigger a session poll (for refresh button)
#[tauri::command]
#[specta::specta]
pub async fn sessions_poll_now(
    poller: State<'_, SessionPollerWrapper>,
) -> Result<Vec<SessionInfo>, String> {
    poller.0.poll_now().await
}

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

    #[test]
    fn test_polling_hint_parsing() {
        // Test valid hints
        assert_eq!(
            match "cast_active" {
                "cast_active" => Some(PollingHint::CastActive),
                "cast_discovery" => Some(PollingHint::CastDiscovery),
                "normal" => Some(PollingHint::Normal),
                _ => None,
            },
            Some(PollingHint::CastActive)
        );

        assert_eq!(
            match "cast_discovery" {
                "cast_active" => Some(PollingHint::CastActive),
                "cast_discovery" => Some(PollingHint::CastDiscovery),
                "normal" => Some(PollingHint::Normal),
                _ => None,
            },
            Some(PollingHint::CastDiscovery)
        );

        assert_eq!(
            match "normal" {
                "cast_active" => Some(PollingHint::CastActive),
                "cast_discovery" => Some(PollingHint::CastDiscovery),
                "normal" => Some(PollingHint::Normal),
                _ => None,
            },
            Some(PollingHint::Normal)
        );
    }

    #[test]
    fn test_invalid_polling_hint() {
        // Test invalid hint
        let result = match "invalid" {
            "cast_active" => Ok(PollingHint::CastActive),
            "cast_discovery" => Ok(PollingHint::CastDiscovery),
            "normal" => Ok(PollingHint::Normal),
            _ => Err("Invalid polling hint"),
        };

        assert!(result.is_err());
    }

    #[test]
    fn test_session_poller_wrapper_structure() {
        // Test that wrapper type structure is correct
        assert!(std::mem::size_of::<SessionPollerWrapper>() > 0);
    }

    #[test]
    fn test_polling_hints_exist() {
        // Verify polling hint variants exist
        let _ = PollingHint::CastActive;
        let _ = PollingHint::CastDiscovery;
        let _ = PollingHint::Normal;
    }
}
