#!/bin/bash
# Test script for Crossplane Proxmox Provider Controller

set -e

echo "=== Crossplane Proxmox Provider Controller Test ==="
echo ""

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Test functions
test_providerconfig() {
    echo -e "${YELLOW}Testing ProviderConfig...${NC}"
    
    if kubectl get providerconfig proxmox-provider-config &>/dev/null; then
        echo -e "${GREEN}✅ ProviderConfig exists${NC}"
        kubectl get providerconfig proxmox-provider-config -o yaml | grep -A 5 "status:" || echo "No status yet"
    else
        echo -e "${RED}❌ ProviderConfig not found${NC}"
        return 1
    fi
}

test_proxmoxvm() {
    echo -e "${YELLOW}Testing ProxmoxVM...${NC}"
    
    if kubectl get proxmoxvm test-vm &>/dev/null; then
        echo -e "${GREEN}✅ ProxmoxVM exists${NC}"
        kubectl get proxmoxvm test-vm -o yaml | grep -A 10 "status:" || echo "No status yet"
    else
        echo -e "${RED}❌ ProxmoxVM not found${NC}"
        return 1
    fi
}

test_controller_logs() {
    echo -e "${YELLOW}Checking controller logs...${NC}"
    
    if kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --tail=20 2>&1 | grep -i error; then
        echo -e "${RED}❌ Errors found in controller logs${NC}"
        kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --tail=20 | grep -i error
        return 1
    else
        echo -e "${GREEN}✅ No errors in recent controller logs${NC}"
    fi
}

test_reconciliation() {
    echo -e "${YELLOW}Testing reconciliation...${NC}"
    
    # Check if controller is reconciling
    RECONCILE_COUNT=$(kubectl logs -n crossplane-system -l app=crossplane-provider-proxmox --tail=100 | grep -i "reconciling\|reconcile" | wc -l)
    
    if [ "$RECONCILE_COUNT" -gt 0 ]; then
        echo -e "${GREEN}✅ Controller is reconciling (${RECONCILE_COUNT} reconcile events found)${NC}"
    else
        echo -e "${YELLOW}⚠️  No reconcile events found in recent logs${NC}"
    fi
}

test_graphql() {
    echo -e "${YELLOW}Testing GraphQL API...${NC}"
    
    if curl -s http://localhost:4000/graphql -X POST -H "Content-Type: application/json" -d '{"query": "{ __typename }"}' &>/dev/null; then
        echo -e "${GREEN}✅ GraphQL API is accessible${NC}"
    else
        echo -e "${YELLOW}⚠️  GraphQL API not accessible on localhost:4000${NC}"
    fi
}

test_frontend() {
    echo -e "${YELLOW}Testing Frontend...${NC}"
    
    if curl -s http://localhost:3000 &>/dev/null; then
        echo -e "${GREEN}✅ Frontend is accessible${NC}"
    else
        echo -e "${YELLOW}⚠️  Frontend not accessible on localhost:3000${NC}"
    fi
}

test_backend_services() {
    echo -e "${YELLOW}Testing Backend Services...${NC}"
    
    # Check for database
    if kubectl get pods -A | grep -E "(postgres|database)" &>/dev/null; then
        echo -e "${GREEN}✅ Database pods found${NC}"
    else
        echo -e "${YELLOW}⚠️  Database pods not found${NC}"
    fi
    
    # Check for API
    if kubectl get pods -A | grep -E "(api|graphql)" &>/dev/null; then
        echo -e "${GREEN}✅ API pods found${NC}"
    else
        echo -e "${YELLOW}⚠️  API pods not found${NC}"
    fi
}

# Run tests
echo "Running tests..."
echo ""

test_providerconfig
echo ""

test_proxmoxvm
echo ""

test_controller_logs
echo ""

test_reconciliation
echo ""

test_graphql
echo ""

test_frontend
echo ""

test_backend_services
echo ""

echo "=== Test Complete ==="

