#!/bin/bash

# AppImage processing and integration tests

# Source the test framework
source "$(dirname "${BASH_SOURCE[0]}")/test_framework.sh"

# Test functions
test_appimage_install_workflow() {
    log_info "Testing AppImage installation workflow"
    
    # Copy a mock AppImage to Downloads
    local test_appimage="$TEST_HOME/Downloads/TestApp-1.0.0-x86_64.AppImage"
    cp "$TEST_ENV_DIR/appimages/samples/TestApp-1.0.0-x86_64.AppImage" "$test_appimage"
    
    # Test installation (simulate user input: choice 1 for first directory)
    local output
    output=$(echo "1" | "$TEST_ENV_DIR/bin/ai_test" install "$test_appimage" 2>&1)
    local exit_code=$?
    
    assert_equal 0 "$exit_code" "Install command should succeed"
    assert_equal 1 "$(echo "$output" | grep -c "Where would you like to store")" "Should ask for storage location"
    assert_equal 1 "$(echo "$output" | grep -c "Processing")" "Should show processing message"
}

test_desktop_file_creation() {
    log_info "Testing desktop file creation"
    
    # Install an AppImage first
    local test_appimage="$TEST_HOME/Downloads/Simple.AppImage"
    cp "$TEST_ENV_DIR/appimages/samples/Simple.AppImage" "$test_appimage"
    
    # Install with choice 0 (keep in current location)
    echo "0" | "$TEST_ENV_DIR/bin/ai_test" install "$test_appimage" &>/dev/null
    
    # Check if desktop file was created
    local desktop_file="$TEST_DESKTOP_DIR/Simple.desktop"
    assert_file_exists "$desktop_file" "Desktop file should be created"
    
    # Check desktop file content
    local desktop_content
    desktop_content=$(cat "$desktop_file")
    
    assert_equal 1 "$(echo "$desktop_content" | grep -c "Name=Simple")" "Desktop file should contain app name"
    assert_equal 1 "$(echo "$desktop_content" | grep -c "Type=Application")" "Desktop file should be application type"
    assert_equal 1 "$(echo "$desktop_content" | grep -c "Generated by AppImage Integrator")" "Desktop file should contain generator comment"
}

test_electron_app_detection() {
    log_info "Testing Electron app detection"
    
    # Install an Electron app
    local test_appimage="$TEST_HOME/Downloads/VSCode-1.85.0.AppImage"
    cp "$TEST_ENV_DIR/appimages/samples/VSCode-1.85.0.AppImage" "$test_appimage"
    
    # Install with choice 0
    echo "0" | "$TEST_ENV_DIR/bin/ai_test" install "$test_appimage" &>/dev/null
    
    # Check if desktop file contains --no-sandbox
    local desktop_file="$TEST_DESKTOP_DIR/VSCode-1.85.0.desktop"
    assert_file_exists "$desktop_file" "Desktop file should be created for Electron app"
    
    local exec_line
    exec_line=$(grep "^Exec=" "$desktop_file")
    
    assert_equal 1 "$(echo "$exec_line" | grep -c -- "--no-sandbox")" "Electron app should have --no-sandbox flag"
}

test_appimage_moving() {
    log_info "Testing AppImage moving functionality"
    
    # Place AppImage in Downloads
    local source_appimage="$TEST_HOME/Downloads/TestMove.AppImage"
    cp "$TEST_ENV_DIR/appimages/samples/TestApp-1.0.0-x86_64.AppImage" "$source_appimage"
    
    # Install with choice 1 (move to AppImages directory)
    echo "1" | "$TEST_ENV_DIR/bin/ai_test" install "$source_appimage" &>/dev/null
    
    # Check if file was moved
    local target_appimage="$TEST_HOME/AppImages/TestMove.AppImage"
    assert_file_exists "$target_appimage" "AppImage should be moved to target directory"
    assert_file_not_exists "$source_appimage" "AppImage should be removed from source directory"
}

test_duplicate_integration_prevention() {
    log_info "Testing duplicate integration prevention"
    
    # Install an AppImage
    local test_appimage="$TEST_HOME/Downloads/DuplicateTest.AppImage"
    cp "$TEST_ENV_DIR/appimages/samples/TestApp-1.0.0-x86_64.AppImage" "$test_appimage"
    
    echo "0" | "$TEST_ENV_DIR/bin/ai_test" install "$test_appimage" &>/dev/null
    
    # Try to find AppImages - should show as already integrated
    local output
    output=$("$TEST_ENV_DIR/bin/ai_test" find 2>&1)
    
    assert_equal 1 "$(echo "$output" | grep -c "DuplicateTest.*already integrated")" "Should detect already integrated AppImage"
}

test_list_integrated_appimages() {
    log_info "Testing list of integrated AppImages"
    
    # Install multiple AppImages
    local apps=("ListTest1.AppImage" "ListTest2.AppImage")
    for app in "${apps[@]}"; do
        local test_appimage="$TEST_HOME/Downloads/$app"
        cp "$TEST_ENV_DIR/appimages/samples/TestApp-1.0.0-x86_64.AppImage" "$test_appimage"
        echo "0" | "$TEST_ENV_DIR/bin/ai_test" install "$test_appimage" &>/dev/null
    done
    
    # List integrated AppImages
    local output
    output=$("$TEST_ENV_DIR/bin/ai_test" list 2>&1)
    
    assert_equal 1 "$(echo "$output" | grep -c "Integrated AppImages")" "Should show integrated AppImages header"
    assert_equal 1 "$(echo "$output" | grep -c "^[0-9]\+\. ListTest1$")" "Should list first integrated AppImage"
    assert_equal 1 "$(echo "$output" | grep -c "^[0-9]\+\. ListTest2$")" "Should list second integrated AppImage"
}

test_appimage_removal() {
    log_info "Testing AppImage removal"
    
    # Install an AppImage
    local test_appimage="$TEST_HOME/Downloads/RemoveTest.AppImage"
    cp "$TEST_ENV_DIR/appimages/samples/TestApp-1.0.0-x86_64.AppImage" "$test_appimage"
    
    echo "0" | "$TEST_ENV_DIR/bin/ai_test" install "$test_appimage" &>/dev/null
    
    # Verify it's installed
    local desktop_file="$TEST_DESKTOP_DIR/RemoveTest.desktop"
    assert_file_exists "$desktop_file" "Desktop file should exist before removal"
    
    # Remove it (simulate user confirmation)
    local output
    output=$(echo "y" | "$TEST_ENV_DIR/bin/ai_test" remove RemoveTest 2>&1)
    
    assert_equal 1 "$(echo "$output" | grep -c "Found: RemoveTest")" "Should find AppImage to remove"
    assert_equal 1 "$(echo "$output" | grep -c "Removed.*integration")" "Should confirm removal"
    assert_file_not_exists "$desktop_file" "Desktop file should be removed"
}

test_logs_functionality() {
    log_info "Testing logs functionality"
    
    # Install an AppImage
    local test_appimage="$TEST_HOME/Downloads/LogsTest.AppImage"
    cp "$TEST_ENV_DIR/appimages/samples/TestApp-1.0.0-x86_64.AppImage" "$test_appimage"
    
    echo "0" | "$TEST_ENV_DIR/bin/ai_test" install "$test_appimage" &>/dev/null
    
    # Create a log file
    local logs_dir="$TEST_CONFIG_DIR/logs"
    mkdir -p "$logs_dir"
    echo "Test log entry" > "$logs_dir/LogsTest.log"
    
    # Test logs command
    local output
    output=$("$TEST_ENV_DIR/bin/ai_test" logs LogsTest 2>&1)
    
    assert_equal 1 "$(echo "$output" | grep -c "Showing logs for")" "Should show logs header"
    assert_equal 1 "$(echo "$output" | grep -c "Test log entry")" "Should show log content"
}

test_config_file_validation() {
    log_info "Testing config file validation"
    
    # Create a custom config
    cat > "$TEST_CONFIG_DIR/config.ini" <<EOF
# Custom test config
icons_dir=$TEST_ICONS_DIR
update_dir=$TEST_DESKTOP_DIR
appimages_dirs=("$TEST_HOME/CustomApps" "$TEST_HOME/Applications")
EOF
    
    # Run status command to load config
    local output
    output=$("$TEST_ENV_DIR/bin/ai_test" status 2>&1)
    
    assert_equal 1 "$(echo "$output" | grep -c "CustomApps")" "Should show custom AppImages directory"
    assert_equal 1 "$(echo "$output" | grep -c "not created yet")" "Should indicate directory doesn't exist"
}

test_version_upgrade_detection() {
    log_info "Testing version upgrade detection"
    
    # Set old version
    echo "1.0.0" > "$TEST_CONFIG_DIR/VERSION"
    
    # Run a command that checks version
    local output
    output=$("$TEST_ENV_DIR/bin/ai_test" status 2>&1)
    
    # Check if upgrade was detected (should update version file)
    local version_content
    version_content=$(cat "$TEST_CONFIG_DIR/VERSION")
    
    local project_version
    project_version=$(get_project_version)
    assert_equal "$project_version" "$version_content" "Version should be updated to current version"
}

test_icon_extraction() {
    log_info "Testing icon extraction"
    
    # Install an AppImage
    local test_appimage="$TEST_HOME/Downloads/IconTest.AppImage"
    cp "$TEST_ENV_DIR/appimages/samples/TestApp-1.0.0-x86_64.AppImage" "$test_appimage"
    
    echo "0" | "$TEST_ENV_DIR/bin/ai_test" install "$test_appimage" &>/dev/null
    
    # Check if icon was extracted (mock AppImage creates icon.png)
    local icon_file="$TEST_ICONS_DIR/icon.png"
    assert_file_exists "$icon_file" "Icon file should be extracted"
    
    # Check if desktop file references the icon
    local desktop_file="$TEST_DESKTOP_DIR/IconTest.desktop"
    local icon_line
    icon_line=$(grep "^Icon=" "$desktop_file")
    
    assert_equal 1 "$(echo "$icon_line" | grep -c "$icon_file")" "Desktop file should reference extracted icon"
}

# Run all tests
run_appimage_processing_tests() {
    log_info "Running AppImage processing tests"
    
    # Initialize test framework
    init_test_framework
    
    # Run individual test functions
    test_appimage_install_workflow
    test_desktop_file_creation
    test_electron_app_detection
    test_appimage_moving
    test_duplicate_integration_prevention
    test_list_integrated_appimages
    test_appimage_removal
    test_logs_functionality
    test_config_file_validation
    test_version_upgrade_detection
    test_icon_extraction
    
    # Print summary
    print_test_summary
}

# Run tests if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    run_appimage_processing_tests
fi