<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

header('Content-Type: application/json');

$host = "localhost";
$db = "togethere_cloud";
$user = "root";
$pass = "HasloDoSQL";

try {
    echo json_encode(['step' => 'Próba połączenia z bazą...']) . "\n";
    
    
$pdo->exec("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
    
    echo json_encode(['step' => 'Połączenie OK', 'success' => true]) . "\n";
    
    // Sprawdzenie tabeli users
    $stmt = $pdo->query("SHOW TABLES LIKE 'users'");
    $userTableExists = $stmt->rowCount() > 0;
    echo json_encode(['users_table_exists' => $userTableExists]) . "\n";
    
    if ($userTableExists) {
        // Sprawdzenie struktury tabeli users
        $stmt = $pdo->query("DESCRIBE users");
        $columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
        echo json_encode(['users_columns' => $columns]) . "\n";
        
        // Sprawdzenie liczby rekordów
        $stmt = $pdo->query("SELECT COUNT(*) as total FROM users");
        $count = $stmt->fetch(PDO::FETCH_ASSOC)['total'];
        echo json_encode(['users_count' => $count]) . "\n";
    }
    
    // Sprawdzenie tabeli user_stats
    $stmt = $pdo->query("SHOW TABLES LIKE 'user_stats'");
    $statsTableExists = $stmt->rowCount() > 0;
    echo json_encode(['user_stats_table_exists' => $statsTableExists]) . "\n";
    
    if ($statsTableExists) {
        $stmt = $pdo->query("DESCRIBE user_stats");
        $columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
        echo json_encode(['user_stats_columns' => $columns]) . "\n";
    }
    
    // Test prostego zapytania
    $stmt = $pdo->query("SELECT id, username, email FROM users LIMIT 1");
    $testUser = $stmt->fetch(PDO::FETCH_ASSOC);
    echo json_encode(['test_user' => $testUser]) . "\n";
    
    echo json_encode(['final' => 'Wszystkie testy przeszły pomyślnie!', 'success' => true]);
    
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'error' => 'PDO Error: ' . $e->getMessage(),
        'code' => $e->getCode()
    ]);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'error' => 'General Error: ' . $e->getMessage()
    ]);
}
?>

