<?php
// Debug script to check fuzzy matching output
include_once 'app/Models/DokumenModel.php';
$db = mysqli_connect('localhost', 'root', '', 'simkp_asn');
$nip = '198205192006041007';

// Logic from Controller
$res = mysqli_query($db, "SELECT * FROM dokumen WHERE nip = '$nip'");
$documents = [];
while ($row = mysqli_fetch_assoc($res)) {
    $documents[] = $row;
}

$existing = [];
foreach ($documents as $doc) {
    $rawName = strtoupper($doc['nama_dokumen']);
    $cleanKey = preg_replace('/[^A-Z0-9]/', '', $rawName);

    if (!empty($cleanKey)) {
        $existing[$cleanKey] = [
            'original_name' => $doc['nama_dokumen']
        ];
    }

    $keywords = [
        'SKP1' => ['REALISASI', 'PERTAMA', '2025'],
        'SKP2' => ['REALISASI', 'KEDUA', '2024'],
        'SKKP' => ['SK', 'KP', 'TERAKHIR'],
        'SKPNS' => ['SK', 'PNS', 'PETIKAN'],
        'SKCPNS' => ['SK', 'CPNS'],
        'UJIAN_DINAS' => ['UJIAN', 'DINAS'],
        'SK_JABATAN_ATASAN' => ['JABATAN', 'ATASAN'],
        'SK_JABATAN_TERAKHIR' => ['JABATAN', 'TERAKHIR'],
        'PELANTIKAN' => ['PELANTIKAN'],
        'TUBEL' => ['TUGAS', 'BELAJAR'],
        'IJAZAH' => ['IJAZAH'],
        'TRANSKRIP' => ['TRANSKRIP'],
        'GELAR' => ['GELAR'],
        'PEMBERHENTIAN' => ['PEMBERHENTIAN'],
        'MUTASI' => ['MUTASI']
    ];

    foreach ($keywords as $keyPrefix => $words) {
        $matchCount = 0;
        foreach ($words as $w) {
            if (strpos($rawName, $w) !== false)
                $matchCount++;
        }
        if ($matchCount >= count($words)) {
            $existing['FUZZY_' . $keyPrefix] = "MATCHED VIA " . $rawName;
        }
    }
}

print_r($existing);
mysqli_close($db);
