<?php
namespace PayCal\Licensing;

defined('ABSPATH') || exit;

class Cron {
  public static function maybe_schedule(): void {
    if (!wp_next_scheduled('paycal_license_revalidate')) {
      wp_schedule_event(time() + 60, 'hourly', 'paycal_license_revalidate');
    }
  }

  public static function revalidate_now(): void {
    $key = LicenseStore::license_key();
    if (!$key) return;

    $res = LicenseClient::validate_remote($key);

    if (!empty($res['ok'])) {
      LicenseStore::set($res['data']);
      return;
    }

    if (($res['reason'] ?? '') === 'invalid') {
      LicenseStore::mark_invalid_with_grace(7);
      return;
    }

    \paycal_debug_log('[PayCal] License revalidation skipped status change after transient validation failure: ' . sanitize_text_field((string) ($res['error'] ?? 'Unknown error')));
  }
}
