#!/bin/bash

# Estate Platform - Quick Verification Script
# Run this to verify all Redis and performance improvements are working

echo "=========================================="
echo "Estate Platform - System Verification"
echo "=========================================="
echo ""

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

# Counter
PASSED=0
FAILED=0

# Function to check a condition
check() {
    if [ $1 -eq 0 ]; then
        echo -e "${GREEN}✓ PASS${NC}: $2"
        ((PASSED++))
    else
        echo -e "${RED}✗ FAIL${NC}: $2"
        ((FAILED++))
    fi
}

echo "1. Checking Prerequisites"
echo "------------------------"

# Check Node.js
node --version > /dev/null 2>&1
check $? "Node.js installed"

# Check npm
npm --version > /dev/null 2>&1
check $? "npm installed"

# Check Docker
docker --version > /dev/null 2>&1
check $? "Docker installed"

# Check Docker Compose
docker-compose --version > /dev/null 2>&1
check $? "Docker Compose installed"

echo ""
echo "2. Checking Redis"
echo "----------------"

# Check if Redis CLI is available
redis-cli --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
    # Try to ping Redis
    redis-cli ping > /dev/null 2>&1
    check $? "Redis server running and accessible"
    
    # Check Redis version
    REDIS_VER=$(redis-cli INFO server | grep redis_version | cut -d: -f2 | tr -d '\r')
    echo "  Redis version: $REDIS_VER"
else
    echo -e "${YELLOW}⚠ INFO${NC}: Redis CLI not found (OK if using Docker)"
fi

echo ""
echo "3. Checking Application Files"
echo "-----------------------------"

# Check if redis.ts exists
[ -f "lib/redis.ts" ]
check $? "Redis client configuration (lib/redis.ts)"

# Check if auth.ts has Redis integration
grep -q "redis" "lib/auth.ts"
check $? "BetterAuth Redis integration (lib/auth.ts)"

# Check if docker-compose has Redis service
grep -q "redis:" "docker-compose.yml"
check $? "Docker Compose Redis service"

# Check if package.json has Redis packages
grep -q "redis" "package.json"
check $? "Redis packages in package.json"

# Check if admin setup API has caching
grep -q "getCached\|setCached" "app/api/admin/setup/route.ts"
check $? "Admin setup API caching"

echo ""
echo "4. Checking Documentation"
echo "------------------------"

# Check for Redis documentation
[ -f "docs/REDIS_SETUP.md" ]
check $? "Redis Setup Guide (docs/REDIS_SETUP.md)"

# Check for Complete Setup Guide
[ -f "docs/COMPLETE_SETUP_GUIDE.md" ]
check $? "Complete Setup Guide (docs/COMPLETE_SETUP_GUIDE.md)"

# Check for environment example
[ -f ".env.example" ]
check $? "Environment template (.env.example)"

echo ""
echo "5. Quick Docker Check"
echo "-------------------"

# Check if docker-compose.yml is valid
docker-compose config > /dev/null 2>&1
check $? "Docker Compose configuration valid"

echo ""
echo "=========================================="
echo "Verification Summary"
echo "=========================================="
echo -e "${GREEN}Passed: $PASSED${NC}"
echo -e "${RED}Failed: $FAILED${NC}"
echo ""

if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}✓ All checks passed!${NC}"
    echo ""
    echo "Next steps:"
    echo "1. Start services: docker-compose up -d"
    echo "2. Initialize database: docker-compose exec web npm run db:migrate"
    echo "3. Access application: http://localhost:3000"
    echo "4. Verify Redis: redis-cli ping"
    exit 0
else
    echo -e "${RED}✗ Some checks failed. Please review above.${NC}"
    echo ""
    echo "Troubleshooting:"
    echo "1. Install missing prerequisites"
    echo "2. Check file permissions"
    echo "3. Verify git clone was successful"
    echo "4. See docs/COMPLETE_SETUP_GUIDE.md for help"
    exit 1
fi
