<?php
// API Logic Simulation Script
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);
define('ROOTPATH', __DIR__ . DIRECTORY_SEPARATOR);

$nip = '198205192006041007';
$existing = [];

// SIMULATE PHYSICAL SCAN
$possiblePaths = [
    FCPATH . 'uploads/dokumen/',
    ROOTPATH . 'public/uploads/dokumen/',
    './uploads/dokumen/',
    './public/uploads/dokumen/' // Added one more for extra safety
];

echo "<h3>Path Testing:</h3><ul>";
foreach ($possiblePaths as $uploadPath) {
    if (is_dir($uploadPath)) {
        echo "<li><strong style='color:green'>FOUND:</strong> $uploadPath</li>";
        $files = scandir($uploadPath);
        foreach ($files as $file) {
            if (strpos($file, $nip) === 0) {
                $fUpper = strtoupper($file);
                $fileData = ['file_path' => $file, 'original_name' => 'Legacy File: ' . $file];

                // Map
                if (strpos($fUpper, 'SKP') !== false) {
                    if (strpos($fUpper, '2025') !== false || strpos($fUpper, 'PERTAMA') !== false)
                        $existing['FUZZY_SKP1'] = $fileData;
                    if (strpos($fUpper, '2024') !== false || strpos($fUpper, 'KEDUA') !== false)
                        $existing['FUZZY_SKP2'] = $fileData;
                }
                if (strpos($fUpper, 'SK_KP') !== false || (strpos($fUpper, 'SK') !== false && strpos($fUpper, 'KP') !== false))
                    $existing['FUZZY_SKKP'] = $fileData;
                if (strpos($fUpper, 'SK_PNS') !== false || strpos($fUpper, 'PNS') !== false)
                    $existing['FUZZY_SKPNS'] = $fileData;
                if (strpos($fUpper, 'SK_CPNS') !== false || strpos($fUpper, 'CPNS') !== false)
                    $existing['FUZZY_SKCPNS'] = $fileData;
                // Add more as needed...
            }
        }
    } else {
        echo "<li><strong style='color:red'>NOT FOUND:</strong> $uploadPath</li>";
    }
}
echo "</ul>";

echo "<h3>Detection Result:</h3><pre>";
print_r($existing);
echo "</pre>";
