#!/bin/bash
# ROA2WEB Integration Test Suite
# Tests all critical system components

set -e

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

log() {
    echo -e "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}

test_passed() {
    log "${GREEN}✅ $1${NC}"
}

test_failed() {
    log "${RED}❌ $1${NC}"
    exit 1
}

test_warning() {
    log "${YELLOW}⚠️  $1${NC}"
}

log "${BLUE}🚀 Starting ROA2WEB Integration Tests${NC}"

# Test 1: Check if all containers are running
log "${BLUE}Testing container status...${NC}"
if docker compose ps | grep -q "roa-ssh-tunnel.*Up.*healthy"; then
    test_passed "SSH tunnel container healthy"
else
    test_failed "SSH tunnel container not healthy"
fi

if docker compose ps | grep -q "roa-frontend.*Up.*healthy"; then
    test_passed "Frontend container healthy"
else
    test_failed "Frontend container not healthy"
fi

if docker compose ps | grep -q "roa-redis.*Up.*healthy"; then
    test_passed "Redis container healthy"
else
    test_failed "Redis container not healthy"
fi

# Test 2: SSH Tunnel Connectivity
log "${BLUE}Testing SSH tunnel connectivity...${NC}"
if docker exec roa-ssh-tunnel nc -z localhost 1521 >/dev/null 2>&1; then
    test_passed "SSH tunnel port 1521 accessible"
else
    test_failed "SSH tunnel port 1521 not accessible"
fi

# Test 3: Backend API Endpoints
log "${BLUE}Testing backend API endpoints...${NC}"
if curl -s http://localhost:8000/health | grep -q "api.*healthy"; then
    test_passed "Backend health endpoint responds"
else
    test_failed "Backend health endpoint not responding"
fi

# Test 4: Frontend Access
log "${BLUE}Testing frontend access...${NC}"
if curl -s http://localhost:3000 | grep -q "ROA Reports"; then
    test_passed "Frontend serves application"
else
    test_failed "Frontend not serving application"
fi

# Test 5: Nginx Gateway Routing
log "${BLUE}Testing nginx gateway routing...${NC}"
if curl -s http://localhost:8080/health | grep -q "healthy"; then
    test_passed "Gateway routes to backend health endpoint"
else
    test_failed "Gateway not routing to backend"
fi

if curl -s http://localhost:8080/ | grep -q "ROA Reports"; then
    test_passed "Gateway routes to frontend"
else
    test_failed "Gateway not routing to frontend"
fi

# Test 6: Redis Connectivity
log "${BLUE}Testing Redis connectivity...${NC}"
if echo "PING" | nc localhost 6379 | grep -q "PONG"; then
    test_passed "Redis responds to PING"
else
    test_failed "Redis not responding to PING"
fi

# Test 7: Oracle Database Connection (Expected to fail with auth error)
log "${BLUE}Testing Oracle database connection...${NC}"
if curl -s http://localhost:8000/health | grep -q "ORA-01017"; then
    test_warning "Oracle connection reaches database but auth failed (expected - need valid password)"
elif curl -s http://localhost:8000/health | grep -q "database.*healthy"; then
    test_passed "Oracle database connection successful"
else
    test_warning "Oracle connection issue - check SSH tunnel and credentials"
fi

# Summary
log "${GREEN}🎉 Integration tests completed successfully!${NC}"
log "${BLUE}System Status Summary:${NC}"
log "✅ SSH Tunnel: Working"
log "✅ Frontend: Working" 
log "✅ Redis: Working"
log "✅ Nginx Gateway: Working (port 8080)"
log "⚠️  Backend: API functional, Oracle auth needs password verification"
log ""
log "${YELLOW}Next Steps:${NC}"
log "1. Verify Oracle password for CONTAFIN_ORACLE user"
log "2. Test application functionality through gateway (http://localhost:8080)"
log "3. Monitor logs for any issues: docker compose logs -f"