<?php
declare(strict_types=1);

function gobao_mind_status(array $env): array {
    return [
        'enabled' => true,
        'version' => '0.10.7',
        'mode' => 'evidence-first',
        'source' => 'api/mind.php',
    ];
}

function gobao_mind_analyze(string $message, array $memory, array $knowledge, ?array $toolBridge, array $env): array {
    $text = mb_substr(trim($message), 0, 600);
    $needsTool = $toolBridge !== null;
    $hasKnowledge = count($knowledge) > 0;
    $recentMemory = array_slice($memory, -6);
    return [
        'enabled' => true,
        'messagePreview' => $text,
        'needsTool' => $needsTool,
        'hasKnowledge' => $hasKnowledge,
        'memoryTurns' => count($recentMemory),
        'rules' => [
            'doNotGuess' => true,
            'requireEvidenceForRealtime' => true,
            'confirmHighRiskActions' => true,
        ],
    ];
}

function gobao_mind_prompt_context(array $mind): string {
    if (empty($mind['enabled'])) {
        return '';
    }
    return "GOBAO mind policy:\n" . json_encode([
        'doNotGuess' => true,
        'useToolResultsFirst' => true,
        'requireEvidenceForRealtime' => true,
        'confirmBeforePaymentOrDestructiveAction' => true,
        'currentTurn' => [
            'needsTool' => (bool)($mind['needsTool'] ?? false),
            'hasKnowledge' => (bool)($mind['hasKnowledge'] ?? false),
            'memoryTurns' => (int)($mind['memoryTurns'] ?? 0),
        ],
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
