#!/bin/bash

# ThrillWiki Automation Test Script
# This script validates all automation components without actually running them

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log() {
    echo -e "${BLUE}[TEST]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[✓]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[!]${NC} $1"
}

log_error() {
    echo -e "${RED}[✗]${NC} $1"
}

# Test counters
TESTS_PASSED=0
TESTS_FAILED=0
TESTS_TOTAL=0

test_case() {
    local name="$1"
    local command="$2"
    
    ((TESTS_TOTAL++))
    log "Testing: $name"
    
    if eval "$command" >/dev/null 2>&1; then
        log_success "$name"
        ((TESTS_PASSED++))
    else
        log_error "$name"
        ((TESTS_FAILED++))
    fi
}

test_case_with_output() {
    local name="$1"
    local command="$2"
    local expected_pattern="$3"
    
    ((TESTS_TOTAL++))
    log "Testing: $name"
    
    local output
    if output=$(eval "$command" 2>&1); then
        if [[ -n "$expected_pattern" && ! "$output" =~ $expected_pattern ]]; then
            log_error "$name (unexpected output)"
            ((TESTS_FAILED++))
        else
            log_success "$name"
            ((TESTS_PASSED++))
        fi
    else
        log_error "$name (command failed)"
        ((TESTS_FAILED++))
    fi
}

log "🧪 Starting ThrillWiki Automation Tests"
echo "======================================"

# Test 1: File Permissions
log "\n📁 Testing File Permissions..."
test_case "CI start script is executable" "[ -x scripts/ci-start.sh ]"
test_case "VM deploy script is executable" "[ -x scripts/vm-deploy.sh ]"
test_case "Webhook listener is executable" "[ -x scripts/webhook-listener.py ]"
test_case "VM manager is executable" "[ -x scripts/unraid/vm-manager.py ]"
test_case "Complete automation script is executable" "[ -x scripts/unraid/setup-complete-automation.sh ]"

# Test 2: Script Syntax
log "\n🔍 Testing Script Syntax..."
test_case "CI start script syntax" "bash -n scripts/ci-start.sh"
test_case "VM deploy script syntax" "bash -n scripts/vm-deploy.sh"
test_case "Setup VM CI script syntax" "bash -n scripts/setup-vm-ci.sh"
test_case "Complete automation script syntax" "bash -n scripts/unraid/setup-complete-automation.sh"
test_case "Webhook listener Python syntax" "python3 -m py_compile scripts/webhook-listener.py"
test_case "VM manager Python syntax" "python3 -m py_compile scripts/unraid/vm-manager.py"

# Test 3: Help Functions
log "\n❓ Testing Help Functions..."
test_case_with_output "VM manager help" "python3 scripts/unraid/vm-manager.py --help" "usage:"
test_case_with_output "Webhook listener help" "python3 scripts/webhook-listener.py --help" "usage:"
test_case_with_output "VM deploy script usage" "scripts/vm-deploy.sh invalid 2>&1" "Usage:"

# Test 4: Configuration Validation
log "\n⚙️  Testing Configuration Validation..."
test_case_with_output "Webhook listener test mode" "python3 scripts/webhook-listener.py --test" "Configuration validation"

# Test 5: Directory Structure
log "\n📂 Testing Directory Structure..."
test_case "Scripts directory exists" "[ -d scripts ]"
test_case "Unraid scripts directory exists" "[ -d scripts/unraid ]"
test_case "Systemd directory exists" "[ -d scripts/systemd ]"
test_case "Docs directory exists" "[ -d docs ]"
test_case "Logs directory created" "[ -d logs ]"

# Test 6: Required Files
log "\n📄 Testing Required Files..."
test_case "ThrillWiki service file exists" "[ -f scripts/systemd/thrillwiki.service ]"
test_case "Webhook service file exists" "[ -f scripts/systemd/thrillwiki-webhook.service ]"
test_case "VM deployment setup doc exists" "[ -f docs/VM_DEPLOYMENT_SETUP.md ]"
test_case "Unraid automation doc exists" "[ -f docs/UNRAID_COMPLETE_AUTOMATION.md ]"
test_case "CI README exists" "[ -f CI_README.md ]"

# Test 7: Python Dependencies
log "\n🐍 Testing Python Dependencies..."
test_case "Python 3 available" "command -v python3"
test_case "Requests module available" "python3 -c 'import requests'"
test_case "JSON module available" "python3 -c 'import json'"
test_case "OS module available" "python3 -c 'import os'"
test_case "Subprocess module available" "python3 -c 'import subprocess'"

# Test 8: System Dependencies
log "\n🔧 Testing System Dependencies..."
test_case "SSH command available" "command -v ssh"
test_case "SCP command available" "command -v scp"
test_case "Bash available" "command -v bash"
test_case "Git available" "command -v git"

# Test 9: UV Package Manager
log "\n📦 Testing UV Package Manager..."
if command -v uv >/dev/null 2>&1; then
    log_success "UV package manager is available"
    ((TESTS_PASSED++))
    test_case "UV version check" "uv --version"
else
    log_warning "UV package manager not found (will be installed during setup)"
    ((TESTS_PASSED++))
fi
((TESTS_TOTAL++))

# Test 10: Django Project Structure
log "\n🌟 Testing Django Project Structure..."
test_case "Django manage.py exists" "[ -f manage.py ]"
test_case "Django settings module exists" "[ -f thrillwiki/settings.py ]"
test_case "PyProject.toml exists" "[ -f pyproject.toml ]"

# Final Results
echo
log "📊 Test Results Summary"
echo "======================"
echo "Total Tests: $TESTS_TOTAL"
echo "Passed: $TESTS_PASSED"
echo "Failed: $TESTS_FAILED"

if [ $TESTS_FAILED -eq 0 ]; then
    echo
    log_success "🎉 All tests passed! The automation system is ready."
    echo
    log "Next steps:"
    echo "1. For complete automation: ./scripts/unraid/setup-complete-automation.sh"
    echo "2. For manual setup: ./scripts/setup-vm-ci.sh"
    echo "3. Read documentation: docs/UNRAID_COMPLETE_AUTOMATION.md"
    exit 0
else
    echo
    log_error "❌ Some tests failed. Please check the issues above."
    exit 1
fi