#!/bin/bash

# Verification script for Sprint 4 implementation
# Checks that all required components are present

echo "========================================"
echo "Sprint 4 - KI-Integration Verifikation"
echo "========================================"
echo ""

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

FAILED=0
PASSED=0

check_file() {
    local file="$1"
    local description="$2"

    if [ -f "$file" ]; then
        echo -e "${GREEN}✓${NC} $description"
        echo "  → $file"
        ((PASSED++))
    else
        echo -e "${RED}✗${NC} $description"
        echo "  → $file (NOT FOUND)"
        ((FAILED++))
    fi
}

check_content() {
    local file="$1"
    local pattern="$2"
    local description="$3"

    if [ -f "$file" ] && grep -q "$pattern" "$file"; then
        echo -e "${GREEN}✓${NC} $description"
        ((PASSED++))
    else
        echo -e "${RED}✗${NC} $description"
        echo "  → Pattern '$pattern' not found in $file"
        ((FAILED++))
    fi
}

echo "1. Core Components"
echo "-------------------"
check_file "compliance/services/llm_provider.py" "LLM Provider Abstraction"
check_file "compliance/services/ai_compliance_assistant.py" "AI Compliance Assistant"
check_file "compliance/api/routes.py" "API Routes"
check_file "compliance/api/schemas.py" "API Schemas"
echo ""

echo "2. LLM Provider Classes"
echo "------------------------"
check_content "compliance/services/llm_provider.py" "class LLMProvider" "Abstrakte LLMProvider Klasse"
check_content "compliance/services/llm_provider.py" "class AnthropicProvider" "AnthropicProvider"
check_content "compliance/services/llm_provider.py" "class SelfHostedProvider" "SelfHostedProvider"
check_content "compliance/services/llm_provider.py" "class MockProvider" "MockProvider"
check_content "compliance/services/llm_provider.py" "def get_llm_provider" "get_llm_provider() Factory"
echo ""

echo "3. AI Assistant Methods"
echo "------------------------"
check_content "compliance/services/ai_compliance_assistant.py" "async def interpret_requirement" "interpret_requirement()"
check_content "compliance/services/ai_compliance_assistant.py" "async def suggest_controls" "suggest_controls()"
check_content "compliance/services/ai_compliance_assistant.py" "async def assess_module_risk" "assess_module_risk()"
check_content "compliance/services/ai_compliance_assistant.py" "async def analyze_gap" "analyze_gap()"
check_content "compliance/services/ai_compliance_assistant.py" "async def batch_interpret_requirements" "batch_interpret_requirements()"
echo ""

echo "4. API Endpoints"
echo "-----------------"
check_content "compliance/api/routes.py" "async def get_ai_status" "GET /ai/status"
check_content "compliance/api/routes.py" "async def interpret_requirement" "POST /ai/interpret"
check_content "compliance/api/routes.py" "async def suggest_controls" "POST /ai/suggest-controls"
check_content "compliance/api/routes.py" "async def assess_module_risk" "POST /ai/assess-risk"
check_content "compliance/api/routes.py" "async def analyze_gap" "POST /ai/gap-analysis"
check_content "compliance/api/routes.py" "async def batch_interpret_requirements" "POST /ai/batch-interpret"
echo ""

echo "5. Pydantic Schemas"
echo "--------------------"
check_content "compliance/api/schemas.py" "class AIStatusResponse" "AIStatusResponse"
check_content "compliance/api/schemas.py" "class AIInterpretationRequest" "AIInterpretationRequest"
check_content "compliance/api/schemas.py" "class AIInterpretationResponse" "AIInterpretationResponse"
check_content "compliance/api/schemas.py" "class AIControlSuggestionRequest" "AIControlSuggestionRequest"
check_content "compliance/api/schemas.py" "class AIControlSuggestionResponse" "AIControlSuggestionResponse"
check_content "compliance/api/schemas.py" "class AIRiskAssessmentRequest" "AIRiskAssessmentRequest"
check_content "compliance/api/schemas.py" "class AIRiskAssessmentResponse" "AIRiskAssessmentResponse"
check_content "compliance/api/schemas.py" "class AIGapAnalysisRequest" "AIGapAnalysisRequest"
check_content "compliance/api/schemas.py" "class AIGapAnalysisResponse" "AIGapAnalysisResponse"
echo ""

echo "6. Environment Variables"
echo "-------------------------"
check_content ".env.example" "COMPLIANCE_LLM_PROVIDER" "COMPLIANCE_LLM_PROVIDER"
check_content ".env.example" "ANTHROPIC_MODEL" "ANTHROPIC_MODEL"
check_content ".env.example" "SELF_HOSTED_LLM_URL" "SELF_HOSTED_LLM_URL"
check_content ".env.example" "COMPLIANCE_LLM_MAX_TOKENS" "COMPLIANCE_LLM_MAX_TOKENS"
echo ""

echo "7. Documentation"
echo "-----------------"
check_file "docs/compliance_ai_integration.md" "Vollständige Dokumentation"
check_file "compliance/README_AI.md" "Quick-Start Guide"
check_file "compliance/SPRINT_4_SUMMARY.md" "Sprint 4 Zusammenfassung"
echo ""

echo "8. Tests"
echo "---------"
check_file "tests/test_compliance_ai.py" "Unit Tests"
check_file "scripts/test_compliance_ai_endpoints.py" "Integration Test Script"
echo ""

echo "9. German Prompts"
echo "------------------"
check_content "compliance/services/ai_compliance_assistant.py" "Du bist ein Compliance-Experte" "Deutscher System Prompt"
check_content "compliance/services/ai_compliance_assistant.py" "Breakpilot" "Breakpilot-spezifisch"
check_content "compliance/services/ai_compliance_assistant.py" "EdTech" "EdTech Kontext"
echo ""

# Summary
echo "========================================"
echo "Zusammenfassung"
echo "========================================"
echo -e "${GREEN}Bestanden: $PASSED${NC}"
echo -e "${RED}Fehlgeschlagen: $FAILED${NC}"
echo ""

if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}✓ Sprint 4 ist vollständig implementiert!${NC}"
    exit 0
else
    echo -e "${RED}✗ Es fehlen noch $FAILED Komponenten${NC}"
    exit 1
fi
