<?php
declare(strict_types=1);

require_once __DIR__ . '/_bootstrap.php';

$master = golder_master_pdo();
$jobId = golder_job_start($master, 'cron_demo_expire');

try {
    golder_ensure_payment_tables($master);

    $st = $master->prepare(
        "UPDATE demo_licenses dl
         INNER JOIN customers c ON c.id = dl.customer_id
         SET dl.status = 'expired',
             dl.cancel_reason = 'auto_expire',
             c.license_status = 'expired'
         WHERE dl.status = 'active'
           AND dl.expires_at < UTC_TIMESTAMP()"
    );
    $st->execute();
    $expired = (int)$st->rowCount();

    golder_job_finish($master, $jobId, 'ok', ['expired_demo_rows' => $expired]);
    echo "cron_demo_expire finished.\n";
} catch (Throwable $e) {
    golder_job_finish($master, $jobId, 'error', ['error' => $e->getMessage()]);
    echo "cron_demo_expire failed: {$e->getMessage()}\n";
    exit(1);
}


