#!/bin/bash

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

echo -e "${BLUE}🧪 Phase 1 Validation Script${NC}"
echo "============================="
echo ""

# Track validation results
VALIDATION_PASSED=true

validate_item() {
    local test_name="$1"
    local test_command="$2"
    local required="$3"
    
    echo -n -e "${BLUE}Testing $test_name:${NC} "
    
    if eval "$test_command" > /dev/null 2>&1; then
        echo -e "${GREEN}✅ PASS${NC}"
        return 0
    else
        if [ "$required" = "required" ]; then
            echo -e "${RED}❌ FAIL (REQUIRED)${NC}"
            VALIDATION_PASSED=false
        else
            echo -e "${YELLOW}⚠️  WARN (OPTIONAL)${NC}"
        fi
        return 1
    fi
}

echo -e "${BLUE}1. Project Structure Validation${NC}"
echo "--------------------------------"

validate_item "Docker Compose file" "[ -f 'docker-compose.yml' ]" "required"
validate_item "Environment file" "[ -f '.env' ]" "required"
validate_item "Start script" "[ -x 'scripts/setup/start.sh' ]" "required"
validate_item "Stop script" "[ -x 'scripts/setup/stop.sh' ]" "required"
validate_item "Status script" "[ -x 'scripts/setup/status.sh' ]" "required"

echo ""
echo -e "${BLUE}2. Service Files Validation${NC}"
echo "----------------------------"

# Check all Python services
services=("requirement-processor" "tech-stack-selector" "architecture-designer" "code-generator" "test-generator" "deployment-manager")
for service in "${services[@]}"; do
    validate_item "$service main.py" "[ -f 'services/$service/src/main.py' ] && [ -s 'services/$service/src/main.py' ]" "required"
    validate_item "$service Dockerfile" "[ -f 'services/$service/Dockerfile' ]" "required"
    validate_item "$service requirements.txt" "[ -f 'services/$service/requirements.txt' ]" "required"
done

# Check API Gateway
validate_item "API Gateway server.js" "[ -f 'services/api-gateway/src/server.js' ] && [ -s 'services/api-gateway/src/server.js' ]" "required"
validate_item "API Gateway package.json" "[ -f 'services/api-gateway/package.json' ]" "required"

echo ""
echo -e "${BLUE}3. Database Scripts Validation${NC}"
echo "-------------------------------"

validate_item "PostgreSQL init script" "[ -f 'databases/scripts/init.sql' ]" "required"
validate_item "PostgreSQL schema script" "[ -f 'databases/scripts/schemas.sql' ]" "required"
validate_item "MongoDB init script" "[ -f 'databases/scripts/mongo-init.js' ]" "required"

echo ""
echo -e "${BLUE}4. Infrastructure Configuration${NC}"
echo "--------------------------------"

validate_item "RabbitMQ config" "[ -f 'infrastructure/rabbitmq/rabbitmq.conf' ]" "required"
validate_item "RabbitMQ definitions" "[ -f 'infrastructure/rabbitmq/definitions.json' ]" "required"
validate_item "RabbitMQ Dockerfile" "[ -f 'infrastructure/rabbitmq/Dockerfile' ]" "required"

echo ""
echo -e "${BLUE}5. Runtime Services Validation${NC}"
echo "-------------------------------"

# Check if services are running
validate_item "Docker daemon" "docker info" "required"
validate_item "PostgreSQL container" "docker-compose ps postgres 2>/dev/null | grep -q Up" "optional"
validate_item "Redis container" "docker-compose ps redis 2>/dev/null | grep -q Up" "optional"
validate_item "MongoDB container" "docker-compose ps mongodb 2>/dev/null | grep -q Up" "optional"
validate_item "RabbitMQ container" "docker-compose ps rabbitmq 2>/dev/null | grep -q Up" "optional"

echo ""
echo -e "${BLUE}6. File Content Validation${NC}"
echo "---------------------------"

# Check if main Python files have content
for service in "${services[@]}"; do
    if [ -f "services/$service/src/main.py" ]; then
        lines=$(wc -l < "services/$service/src/main.py" 2>/dev/null || echo "0")
        if [ "$lines" -gt 100 ]; then
            echo -e "${BLUE}$service line count:${NC} ${GREEN}✅ $lines lines${NC}"
        else
            echo -e "${BLUE}$service line count:${NC} ${RED}❌ $lines lines (too few)${NC}"
            VALIDATION_PASSED=false
        fi
    else
        echo -e "${BLUE}$service main.py:${NC} ${RED}❌ File missing${NC}"
        VALIDATION_PASSED=false
    fi
done

# Check API Gateway
if [ -f "services/api-gateway/src/server.js" ]; then
    api_lines=$(wc -l < "services/api-gateway/src/server.js" 2>/dev/null || echo "0")
    if [ "$api_lines" -gt 50 ]; then
        echo -e "${BLUE}API Gateway line count:${NC} ${GREEN}✅ $api_lines lines${NC}"
    else
        echo -e "${BLUE}API Gateway line count:${NC} ${RED}❌ $api_lines lines (too few)${NC}"
        VALIDATION_PASSED=false
    fi
else
    echo -e "${BLUE}API Gateway server.js:${NC} ${RED}❌ File missing${NC}"
    VALIDATION_PASSED=false
fi

echo ""
echo -e "${BLUE}7. Validation Summary${NC}"
echo "--------------------"

if [ "$VALIDATION_PASSED" = true ]; then
    echo -e "${GREEN}✅ ALL VALIDATIONS PASSED!${NC}"
    echo ""
    echo -e "${GREEN}🎉 Phase 1 is complete and ready!${NC}"
    echo ""
    echo -e "${BLUE}Next steps:${NC}"
    echo "  1. Start services: ./scripts/setup/start.sh"
    echo "  2. Verify functionality: ./scripts/setup/status.sh"
    echo "  3. Test databases: ./scripts/setup/dev.sh test-db"
    echo "  4. Begin Phase 2 development"
    echo ""
    exit 0
else
    echo -e "${RED}❌ VALIDATION FAILED!${NC}"
    echo ""
    echo -e "${YELLOW}Please fix the issues above before proceeding.${NC}"
    echo ""
    exit 1
fi
