<?php
// chatbot/test_load.php
header('Content-Type: text/plain; charset=utf-8');

$root = $_SERVER['DOCUMENT_ROOT'];
echo "Checking Environment...\n";
echo "DOCUMENT_ROOT: $root\n";
echo "__DIR__ (here): " . __DIR__ . "\n";

$targetFile = __DIR__ . '/rag/data/vectors.json';
echo "Target File: $targetFile\n";

if (file_exists($targetFile)) {
    echo "Status: FOUND\n";
    echo "Size: " . filesize($targetFile) . " bytes\n";
    echo "Permissions: " . substr(sprintf('%o', fileperms($targetFile)), -4) . "\n";
    
    // Try reading
    $start = microtime(true);
    $content = file_get_contents($targetFile);
    $readTime = microtime(true) - $start;
    echo "Read Time: " . number_format($readTime, 4) . "s\n";
    
    // Try decoding
    ini_set('memory_limit', '512M');
    echo "Memory Limit: " . ini_get('memory_limit') . "\n";
    
    $start = microtime(true);
    $data = json_decode($content, true);
    $decodeTime = microtime(true) - $start;
    
    if ($data === null) {
        echo "JSON Decode: FAILED. Error: " . json_last_error_msg() . "\n";
    } else {
        echo "JSON Decode: SUCCESS. Items: " . count($data) . "\n";
        echo "Decode Time: " . number_format($decodeTime, 4) . "s\n";
    }

    // Check directory list
    echo "\n=== Testing VectorSearch Class ===\n";
    $searchPhp = __DIR__ . '/rag/search.php';
    if (!file_exists($searchPhp)) {
        echo "rag/search.php NOT FOUND at $searchPhp\n";
    } else {
        require_once $searchPhp;
        try {
            $vs = new VectorSearch();
            echo "VectorSearch Instantiated.\n";
            echo "Vector Count from Class: " . $vs->getVectorCount() . "\n";
            
            // Debug path inside class? We can't see private var, but getVectorCount result is enough.
        } catch (Exception $e) {
            echo "VectorSearch Exception: " . $e->getMessage() . "\n";
        }
    }
}
?>
