#!/bin/bash
# CleverAgents v3.7.0 Production Readiness Validation Script
# Run this to verify all remediation steps have been properly implemented

# Don't exit on error - we want to see all results
set +e

echo "=========================================="
echo "CleverAgents Production Readiness Validator"
echo "=========================================="
echo ""

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

# Counters
PASS=0
FAIL=0
WARN=0

# Function to check a condition
check() {
    local description="$1"
    local command="$2"
    local severity="${3:-error}"  # error or warning
    
    echo -n "Checking: $description... "
    
    if eval "$command" > /dev/null 2>&1; then
        echo -e "${GREEN}PASS${NC}"
        ((PASS++))
    else
        if [ "$severity" = "warning" ]; then
            echo -e "${YELLOW}WARNING${NC}"
            ((WARN++))
        else
            echo -e "${RED}FAIL${NC}"
            ((FAIL++))
        fi
    fi
}

echo "Phase 1: Critical Safety Checks"
echo "-------------------------------"

# Check that no agent is calling forgejo_merge_pull_request with force_merge
check "No force_merge parameter usage" \
    "! find .opencode/agents -name '*.md' -exec grep -l 'force_merge.*:.*true' {} \\; | grep -v 'system-watchdog.md' | grep -q ."

# Check for merge safety wrapper
check "Merge safety wrapper exists" \
    "test -f .opencode/agents/shared/merge_safety.md"

# Check for coordination protocols
check "Coordination protocols exist" \
    "test -f .opencode/agents/shared/coordination_protocols.md"

# Check for credential security
check "Credential security guidelines exist" \
    "test -f .opencode/agents/shared/credential_security.md"

echo ""
echo "Phase 2: Compliance Checks"
echo "--------------------------"

# Check key agents for CONTRIBUTING.md compliance
for agent in implementer.md implementation-worker.md behave-tester.md robot-tester.md \
             pr-self-reviewer.md architect.md epic-planner.md; do
    check "$agent has CONTRIBUTING.md reference" \
        "grep -q 'CONTRIBUTING.md' .opencode/agents/$agent"
done

# Check for shared utilities
check "Error handling utilities exist" \
    "test -f .opencode/agents/shared/error_handling.md"

check "Session state utilities exist" \
    "test -f .opencode/agents/shared/session_state.md"

check "Logging utilities exist" \
    "test -f .opencode/agents/shared/logging.md"

# Check for agent behavior tests
check "Agent contract tests exist" \
    "test -f features/agent_contracts.feature"

echo ""
echo "Phase 3: Configuration Checks"
echo "-----------------------------"

# Check for centralized configuration
check "Model configuration exists" \
    "test -f .opencode/agents/config/models.yaml"

check "Resource configuration exists" \
    "test -f .opencode/agents/config/resources.yaml"

# Check for performance monitoring
check "Performance monitoring utilities exist" \
    "test -f .opencode/agents/shared/performance_monitoring.md"

# Check for production readiness checklist
check "Production readiness checklist exists" \
    "test -f .opencode/PRODUCTION_READINESS_CHECKLIST.md"

echo ""
echo "Phase 4: Agent-Specific Checks"
echo "------------------------------"

# Check implementation-orchestrator for resource caps
check "implementation-orchestrator has resource caps" \
    "grep -q 'CA_MAX_PARALLEL_WORKERS' .opencode/agents/implementation-orchestrator.md"

# Check human-liaison for timeout handling
check "human-liaison has timeout handling" \
    "grep -q 'HUMAN_RESPONSE_TIMEOUTS' .opencode/agents/human-liaison.md"

# Check system-watchdog for graceful degradation
check "system-watchdog has graceful degradation" \
    "grep -q 'audit_system_health_and_degradation' .opencode/agents/system-watchdog.md"

echo ""
echo "Phase 5: Integration Checks"
echo "---------------------------"

# Check for proper imports in key agents
check "implementation-worker imports safe merge" \
    "grep -q 'safe_merge_pr' .opencode/agents/implementation-worker.md"

# Warning checks (non-critical but recommended)
echo ""
echo "Phase 6: Best Practice Checks (Warnings)"
echo "----------------------------------------"

# Check all agents have proper mode settings
check "All agents have mode specified" \
    "! grep -L '^mode:' .opencode/agents/*.md | grep -q ." \
    "warning"

# Check all agents have descriptions
check "All agents have descriptions" \
    "! grep -L '^description:' .opencode/agents/*.md | grep -q ." \
    "warning"

echo ""
echo "=========================================="
echo "Validation Summary"
echo "=========================================="
echo -e "Passed:   ${GREEN}$PASS${NC}"
echo -e "Failed:   ${RED}$FAIL${NC}"
echo -e "Warnings: ${YELLOW}$WARN${NC}"
echo ""

if [ $FAIL -eq 0 ]; then
    echo -e "${GREEN}✓ All critical checks passed!${NC}"
    echo ""
    echo "The CleverAgents system has been successfully remediated for v3.7.0 production readiness."
    echo ""
    echo "Next steps:"
    echo "1. Run the agent contract tests: nox -s unit_tests -- features/agent_contracts.feature"
    echo "2. Deploy the updated agents to the autonomous system"
    echo "3. Monitor the system-watchdog for any new findings"
    echo "4. Review any warnings above for additional improvements"
    exit 0
else
    echo -e "${RED}✗ Critical checks failed!${NC}"
    echo ""
    echo "The system is NOT ready for v3.7.0 production deployment."
    echo "Please address the failed checks above before proceeding."
    exit 1
fi