<?php
session_start();
require_once __DIR__ . '/../config.php';

// Check if user is logged in
if (!isset($_SESSION['token'])) {
    header('Location: login.php');
    exit;
}

$token = $_SESSION['token'];

// Get user profile data
$profile_result = api_get_profil($token);

echo "<h1>Debug Profil - Cek Koordinat Kantor</h1>";
echo "<p><strong>Token:</strong> " . substr($token, 0, 20) . "...</p>";

echo "<h2>Profile API Response</h2>";
echo "<p><strong>Success:</strong> " . ($profile_result['success'] ? 'YES' : 'NO') . "</p>";
echo "<p><strong>HTTP Code:</strong> " . $profile_result['http_code'] . "</p>";

echo "<h3>Raw Profile Data:</h3>";
echo "<pre style='background: #f5f5f5; padding: 15px; border-radius: 5px; overflow-x: auto;'>";
echo htmlspecialchars(json_encode($profile_result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
echo "</pre>";

if ($profile_result['success'] && isset($profile_result['data'])) {
    $data = $profile_result['data'];

    echo "<h2>Struktur Data Profil</h2>";

    // Cek struktur data
    echo "<h3>Keys di data utama:</h3>";
    echo "<ul>";
    foreach (array_keys($data) as $key) {
        echo "<li><strong>$key</strong> - " . gettype($data[$key]) . "</li>";
    }
    echo "</ul>";

    // Cek pegawai
    if (isset($data['pegawai'])) {
        echo "<h3>Data Pegawai:</h3>";
        echo "<pre style='background: #e8f4fd; padding: 10px; border-radius: 5px;'>";
        echo htmlspecialchars(json_encode($data['pegawai'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
        echo "</pre>";

        // Cek keys di pegawai
        echo "<h4>Keys di pegawai:</h4>";
        echo "<ul>";
        foreach (array_keys($data['pegawai']) as $key) {
            echo "<li><strong>$key</strong> - " . gettype($data['pegawai'][$key]) . "</li>";
        }
        echo "</ul>";

        // Cek kantor
        if (isset($data['pegawai']['kantor'])) {
            echo "<h3>Data Kantor:</h3>";
            echo "<pre style='background: #e8f5e8; padding: 10px; border-radius: 5px;'>";
            echo htmlspecialchars(json_encode($data['pegawai']['kantor'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
            echo "</pre>";

            // Cek keys di kantor
            echo "<h4>Keys di kantor:</h4>";
            echo "<ul>";
            foreach (array_keys($data['pegawai']['kantor']) as $key) {
                $value = $data['pegawai']['kantor'][$key];
                echo "<li><strong>$key</strong> - " . gettype($value) . " = " . (is_string($value) ? htmlspecialchars($value) : json_encode($value)) . "</li>";
            }
            echo "</ul>";

            // Cek koordinat khusus
            echo "<h3>Koordinat Kantor:</h3>";
            $kantor = $data['pegawai']['kantor'];
            echo "<ul>";
            echo "<li><strong>latitude:</strong> " . (isset($kantor['latitude']) ? $kantor['latitude'] : 'TIDAK ADA') . "</li>";
            echo "<li><strong>lat:</strong> " . (isset($kantor['lat']) ? $kantor['lat'] : 'TIDAK ADA') . "</li>";
            echo "<li><strong>longitude:</strong> " . (isset($kantor['longitude']) ? $kantor['longitude'] : 'TIDAK ADA') . "</li>";
            echo "<li><strong>lng:</strong> " . (isset($kantor['lng']) ? $kantor['lng'] : 'TIDAK ADA') . "</li>";
            echo "<li><strong>toleransi:</strong> " . (isset($kantor['toleransi']) ? $kantor['toleransi'] : 'TIDAK ADA') . "</li>";
            echo "<li><strong>nama_kantor:</strong> " . (isset($kantor['nama_kantor']) ? htmlspecialchars($kantor['nama_kantor']) : 'TIDAK ADA') . "</li>";
            echo "<li><strong>alamat_kantor:</strong> " . (isset($kantor['alamat_kantor']) ? htmlspecialchars($kantor['alamat_kantor']) : 'TIDAK ADA') . "</li>";
            echo "</ul>";
        } else {
            echo "<p style='color: red;'><strong>KANTOR TIDAK DITEMUKAN di pegawai</strong></p>";
        }
    } else {
        echo "<p style='color: red;'><strong>PEGAWAI TIDAK DITEMUKAN</strong></p>";
    }

    // Cek struktur alternatif
    echo "<h2>Pencarian Koordinat di Semua Level</h2>";

    function searchCoordinates($data, $path = '')
    {
        $found = [];
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                $currentPath = $path ? "$path.$key" : $key;
                if (in_array($key, ['latitude', 'lat', 'longitude', 'lng', 'toleransi'])) {
                    $found[$currentPath] = $value;
                } elseif (is_array($value)) {
                    $found = array_merge($found, searchCoordinates($value, $currentPath));
                }
            }
        }
        return $found;
    }

    $coordinates = searchCoordinates($data);
    if (!empty($coordinates)) {
        echo "<h3>Koordinat yang ditemukan:</h3>";
        echo "<ul>";
        foreach ($coordinates as $path => $value) {
            echo "<li><strong>$path:</strong> $value</li>";
        }
        echo "</ul>";
    } else {
        echo "<p style='color: red;'><strong>TIDAK ADA KOORDINAT DITEMUKAN di seluruh struktur data</strong></p>";
    }
}

echo "<hr>";
echo "<p><a href='rekam.php'>← Kembali ke Rekam Presensi</a> | <a href='debug_presensi_submit.php'>Debug Presensi Submit</a></p>";
