<?php
declare(strict_types=1);

namespace Ekesto\CacheWarmup\Support;

/**
 * Access control shim.
 *
 * ECW does NOT own the access-control resolver. It delegates to
 * \Ekesto\CacheAutopilot\AccessControl when ECI is active; when ECI is absent
 * the shim falls back to admin-only (is_super_admin OR manage_options).
 *
 * ECW call sites must ALWAYS go through this shim — never call the
 * shared Cache Autopilot class directly.
 */
final class Access
{
    public static function userCanManage(?int $user_id = null): bool
    {
        if (class_exists(\Ekesto\CacheAutopilot\AccessControl::class)) {
            return \Ekesto\CacheAutopilot\AccessControl::userCanManage($user_id);
        }
        return self::adminOnlyFallback($user_id);
    }

    private static function adminOnlyFallback(?int $user_id): bool
    {
        $uid = $user_id ?? (function_exists('get_current_user_id') ? (int) get_current_user_id() : 0);
        if ($uid <= 0) {
            return false;
        }
        return is_super_admin($uid) || user_can($uid, 'manage_options');
    }
}
