<?php
session_start();
require_once 'includes/functions.php';
requireLogin();

$user = currentUser();
$filesRaw = file_get_contents(DATA_DIR . 'files.json');
$filesData = json_decode($filesRaw, true);

echo "<h2>Debug - DataStore</h2>";
echo "<h3>Kullanıcı Bilgisi</h3>";
echo '<pre>';
var_dump($user ? ['id' => $user['id'], 'username' => $user['username'], 'display_name' => $user['display_name'] ?? ''] : 'Giriş yapılmamış');
echo '</pre>';

echo "<h3>files.json Ham Veri</h3>";
echo '<pre>';
echo htmlspecialchars($filesRaw);
echo '</pre>';

echo "<h3>files.json Parse Edilmiş</h3>";
echo '<pre>';
echo 'Toplam dosya sayısı: ' . count($filesData['files'] ?? []) . "\n";
foreach (($filesData['files'] ?? []) as $f) {
    $owner = $f['owner_id'] ?? 'yok';
    echo "  ID: {$f['id']}, Ad: {$f['original_name']}, Area: {$f['area']}, Owner: $owner, Yüklenme: {$f['uploaded_at']}\n";
}
echo '</pre>';

echo "<h3>getCommonFiles() Sonucu</h3>";
echo '<pre>';
$common = getCommonFiles();
echo 'Dönen dosya sayısı: ' . count($common) . "\n";
foreach ($common as $f) {
    $owner = $f['owner_id'] ?? 'yok';
    echo "  ID: {$f['id']}, Ad: {$f['original_name']}, Area: {$f['area']}, Owner: $owner\n";
}
echo '</pre>';

echo "<h3>DATA_DIR</h3>";
echo '<pre>' . DATA_DIR . '</pre>';

echo "<h3>__FILE__</h3>";
echo '<pre>' . __FILE__ . '</pre>';

echo "<h3>SESSION</h3>";
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
