use assert_cmd::Command;
use predicates::prelude::*;

#[test]
fn test_version_flag() {
    let mut cmd = Command::cargo_bin("tango").unwrap();
    cmd.arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::starts_with("tango"));
}

#[test]
fn test_help_flag() {
    let mut cmd = Command::cargo_bin("tango").unwrap();
    cmd.arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("CLI tool"));
}

#[test]
fn test_dependency_check_missing_tmux() {
    if which::which("tmux").is_ok() {
        return; // Skip this test if tmux is available
    }
    
    let mut cmd = Command::cargo_bin("tango").unwrap();
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("tmux is not installed"));
}


#[cfg(test)]
mod tmux_operations {
    use tmux_tango::tmux::TmuxClient;
    use tmux_tango::error::TmuxFzfError;
    
    fn ensure_no_test_sessions() {
        let mut client = TmuxClient::new();
        let _ = client.kill_session("test-session-1");
        let _ = client.kill_session("test-session-2");
        let _ = client.kill_session("test-renamed");
    }
    
    #[test]
    fn test_tmux_session_parsing() {
        let session_line = "test-session: 3 windows (created Mon Jan  1 12:00:00 2024)";
        let session = tmux_tango::tmux::TmuxSession::from_line(session_line);
        
        assert_eq!(session.name.as_ref(), "test-session");
        assert_eq!(session.windows.as_ref(), "3");
        assert_eq!(session.created.as_ref(), " (created Mon Jan  1 12:00:00 2024)");
        assert_eq!(session.raw_line.as_ref(), session_line);
    }

    #[test]
    fn test_tmux_session_parsing_single_window() {
        let session_line = "work: 1 window (created Mon Jan  1 12:00:00 2024)";
        let session = tmux_tango::tmux::TmuxSession::from_line(session_line);
        
        assert_eq!(session.name.as_ref(), "work");
        assert_eq!(session.windows.as_ref(), "1");
        assert_eq!(session.created.as_ref(), " (created Mon Jan  1 12:00:00 2024)");
    }

    #[test]
    fn test_tmux_session_parsing_minimal() {
        let session_line = "minimal:";
        let session = tmux_tango::tmux::TmuxSession::from_line(session_line);
        
        assert_eq!(session.name.as_ref(), "minimal");
        assert_eq!(session.windows.as_ref(), "1");  // Default value when no window info
        assert_eq!(session.created.as_ref(), "unknown");
    }

    #[test]
    fn test_tmux_session_parsing_no_colon() {
        let session_line = "noseparator";
        let session = tmux_tango::tmux::TmuxSession::from_line(session_line);
        
        assert_eq!(session.name.as_ref(), "noseparator");
        assert_eq!(session.windows.as_ref(), "1");
        assert_eq!(session.created.as_ref(), "unknown");
    }
    
    #[test]
    fn test_session_operations() {
        if which::which("tmux").is_err() {
            return; // Skip if tmux not available
        }
        
        ensure_no_test_sessions();
        
        let mut client = TmuxClient::new();
        
        // Test session creation (skip if not in terminal)
        let result = client.new_session(Some("test-session-1"));
        if result.is_err() {
            return; // Skip test if we can't create sessions (e.g., not in terminal)
        }
        
        // Test session listing
        let sessions = client.list_sessions();
        assert!(sessions.is_ok(), "Failed to list sessions: {:?}", sessions);
        let sessions = sessions.unwrap();
        assert!(sessions.iter().any(|s| s.name.as_ref() == "test-session-1"));
        
        // Test session renaming
        let rename_result = client.rename_session("test-session-1", "test-renamed");
        assert!(rename_result.is_ok(), "Failed to rename session: {:?}", rename_result);
        
        // Verify rename worked
        let sessions = client.list_sessions().unwrap();
        assert!(sessions.iter().any(|s| s.name.as_ref() == "test-renamed"));
        assert!(!sessions.iter().any(|s| s.name.as_ref() == "test-session-1"));
        
        // Test session killing
        let kill_result = client.kill_session("test-renamed");
        assert!(kill_result.is_ok(), "Failed to kill session: {:?}", kill_result);
        
        // Verify session was killed
        let sessions = client.list_sessions();
        if let Ok(sessions) = sessions {
            assert!(!sessions.iter().any(|s| s.name.as_ref() == "test-renamed"));
        }
        
        ensure_no_test_sessions();
    }
    
    #[test]
    fn test_invalid_session_operations() {
        if which::which("tmux").is_err() {
            return;
        }
        
        let mut client = TmuxClient::new();
        
        // Test killing non-existent session
        let result = client.kill_session("non-existent-session");
        assert!(result.is_err());
        
        // Test renaming non-existent session
        let result = client.rename_session("non-existent", "new-name");
        assert!(result.is_err());
        
        // Test empty session name for rename
        let result = client.rename_session("any-session", "");
        assert!(matches!(result, Err(TmuxFzfError::InvalidSessionName(_))));
        
        // Test session name with colon for rename
        let result = client.rename_session("any-session", "bad:name");
        assert!(matches!(result, Err(TmuxFzfError::InvalidSessionName(_))));
        
        // Test empty session name for creation
        let result = client.new_session(Some(""));
        assert!(matches!(result, Err(TmuxFzfError::InvalidSessionName(_))));
        
        // Test session name with colon for creation
        let result = client.new_session(Some("bad:name"));
        assert!(matches!(result, Err(TmuxFzfError::InvalidSessionName(_))));
    }
    
    #[test]
    fn test_tmux_environment_detection() {
        let client = TmuxClient::new();
        
        // This will be false when running tests outside tmux
        let inside_tmux = client.is_inside_tmux();
        assert_eq!(inside_tmux, std::env::var("TMUX").is_ok());
    }
}