<?php

namespace FleekDash\Core;

if (!function_exists('fleekdash_log_error')) {
    require_once FLEEKDASH_PLUGIN_DIR . 'includes/Core/debug-helpers.php';
}

/**
 * Admin permission checks for debug REST endpoints.
 * Extracted from DebugController for S1448 compliance.
 */
class DebugPermissionChecker
{
    /**
     * @return bool
     */
    public function check()
    {
        try {
            if (!class_exists('FleekDash\\Core\\AuthMiddleware')) {
                require_once FLEEKDASH_PLUGIN_DIR . 'includes/Core/AuthMiddleware.php';
            }

            $auth = AuthMiddleware::check('admin');

            if (!$auth['authenticated']) {
                fleekdash_log_error(sprintf(
                    'FleekDash Security: Unauthorized debug access - IP: %s, Reason: %s',
                    $_SERVER['REMOTE_ADDR'] ?? 'unknown',
                    $auth['reason'] ?? 'unknown'
                ));
                return false;
            }

            return true;
        } catch (\Exception $e) {
            fleekdash_log_error('FleekDash Security: Auth check failed - ' . $e->getMessage());
            return false;
        }
    }
}
