stt-cli 0.2.1

Speech to text Cli using Groq API and OpenAI API
use super::{PlatformHandler, Error, WindowInfo};
use anyhow::{Result, Context};

#[cfg(target_os = "linux")]
use crate::platform::linux::{x11, wayland};

/// Platform detection and handler factory implementation
#[derive(Debug)]
pub struct PlatformDetector;

impl PlatformDetector {
    /// Detect current platform and return appropriate handler
    pub fn detect() -> Result<Box<dyn PlatformHandler>> {
        #[cfg(target_os = "linux")]
        {
            let session_type = std::env::var("XDG_SESSION_TYPE")
                .unwrap_or_else(|_| "x11".into())
                .to_lowercase();

            // First try Wayland if detected
            if session_type.contains("wayland") {
                match wayland::LinuxWaylandHandler::new() {
                    Ok(handler) => return Ok(handler),
                    Err(e) => tracing::warn!("Wayland detection failed: {}", e),
                }
            }

            // Fallback to X11
            match x11::LinuxX11Handler::new() {
                Ok(handler) => Ok(handler),
                Err(e) => {
                    tracing::error!("X11 detection failed: {}", e);
                    Err(Error::PlatformError(
                        "No supported display server found".into()
                    ).into())
                }
            }
        }

        #[cfg(target_os = "windows")]
        Err(Error::PlatformError("Windows support not implemented".into()).into())

        #[cfg(target_os = "macos")]
        Err(Error::PlatformError("macOS support not implemented".into()).into())

        #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
        Err(Error::UnsupportedPlatform.into())
    }
}

/// Factory function to get platform-specific handler
pub fn get_platform_handler() -> Result<Box<dyn PlatformHandler>> {
    PlatformDetector::detect().map_err(|e| {
        tracing::error!("Platform detection failed: {}", e);
        e
    })
}