<?php
$pdo = new PDO("mysql:host=host700513.hostido.net.pl;dbname=host700513_pomysloweprezenty-pl;charset=utf8", 'host700513_pomysloweprezenty-pl', 'QBVbveHAzR78UN8pc7Um');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

echo "=== CRON JOBS TABLE COLUMNS ===\n";
$stmt = $pdo->query("DESCRIBE pp_cron_jobs");
foreach ($stmt as $row) echo "  " . $row['Field'] . " (" . $row['Type'] . ")\n";

echo "\n=== RECENT CRON JOBS (last 20) ===\n";
$stmt = $pdo->query("SELECT * FROM pp_cron_jobs ORDER BY id DESC LIMIT 20");
foreach ($stmt as $row) {
    echo "  #" . $row['id'] . " type=" . $row['job_type'] . " status=" . $row['status'] . " created=" . ($row['created_at'] ?? $row['date'] ?? 'n/a') . "\n";
}

echo "\n=== PENDING CRON JOBS ===\n";
$stmt = $pdo->query("SELECT * FROM pp_cron_jobs WHERE status = 'pending' ORDER BY id");
foreach ($stmt as $row) {
    echo "  #" . $row['id'] . " type=" . $row['job_type'];
    foreach ($row as $k => $v) if ($v !== null && $k !== 'id' && $k !== 'job_type') echo " $k=$v";
    echo "\n";
}

echo "\n=== NULL ORDERS SINCE 2026-03-15 (after last successful sync) ===\n";
$stmt = $pdo->query("SELECT id, date_order, status FROM pp_shop_orders WHERE apilo_order_id IS NULL AND date_order >= '2026-03-15 14:00:00' ORDER BY date_order ASC");
$rows = $stmt->fetchAll();
echo "  Count: " . count($rows) . "\n";
foreach ($rows as $row) {
    echo sprintf("  #%s  date=%s  status=%s\n", $row['id'], $row['date_order'], $row['status']);
}

echo "\n=== NULL ORDERS COUNT BY DATE RANGE ===\n";
$stmt = $pdo->query("SELECT
    SUM(CASE WHEN date_order >= '2026-03-15 14:00:00' THEN 1 ELSE 0 END) as since_break,
    SUM(CASE WHEN date_order >= '2024-08-20' AND date_order < '2026-03-15 14:00:00' THEN 1 ELSE 0 END) as before_break_after_sync_start,
    SUM(CASE WHEN date_order < '2024-08-20' THEN 1 ELSE 0 END) as before_sync_start
FROM pp_shop_orders WHERE apilo_order_id IS NULL");
$row = $stmt->fetch();
echo "  Since break (2026-03-15 14:00+): " . $row['since_break'] . "\n";
echo "  Before break, after sync_start: " . $row['before_break_after_sync_start'] . "\n";
echo "  Before sync_start (2024-08-20): " . $row['before_sync_start'] . "\n";

echo "\n=== LAST SUCCESSFUL SEND ORDER JOB ===\n";
$stmt = $pdo->query("SELECT * FROM pp_cron_jobs WHERE job_type = 'apilo_send_order' AND status = 'completed' ORDER BY id DESC LIMIT 3");
foreach ($stmt as $row) {
    foreach ($row as $k => $v) if ($v !== null) echo "  $k=$v";
    echo "\n";
}

echo "\nDone.\n";
