<?php
/**
 * Menu Debug API
 *
 * Exposes consolidated debug structure for menu overrides + current menu snapshot.
 *
 * @package FleekDash
 * @since V2.2
 */

namespace FleekDash\API;

use WP_REST_Request;
use WP_REST_Response;
use FleekDash\Admin\Menu\MenuOverridesService;
use FleekDash\Admin\Menu\MenuGeneratorService;
use FleekDash\Core\AuthMiddleware;

class MenuDebugAPI
{
    private MenuOverridesService $menuOverrides;
    private MenuGeneratorService $menuGenerator;

    public function __construct()
    {
        $this->menuOverrides = new MenuOverridesService();
        $this->menuGenerator = new MenuGeneratorService();
    }

    public function check_permissions(): bool // NOSONAR php:S100 — matches permission_callback in MenuDataAPI routes
    {
        return true;
    }

    public function getCompleteDebugStructure(WP_REST_Request $request): WP_REST_Response
    {
        $route_meta = array('rest_route' => $request->get_route());

        try {
            $auth = AuthMiddleware::check('admin');
            if (empty($auth['authenticated'])) {
                return new WP_REST_Response([
                    'success' => false,
                    'message' => 'Access limited',
                    'meta' => $route_meta,
                    'fallback_data' => [
                        'baseConfig' => null,
                        'roleOverrides' => [],
                        'completeStructure' => [],
                        'version' => 0,
                        'rawData' => [
                            'completeStructure' => [],
                            'roleOverrides' => [],
                        ],
                        'menu' => [],
                        'roles' => \wp_roles()->roles,
                    ],
                ], 200);
            }

            $complete_structure = $this->menuOverrides->getAllRoleOverrides();
            $role_overrides = array_filter(
                $complete_structure,
                static function ($overrides) {
                    return !empty($overrides);
                }
            );

            $menu_items = [];
            try {
                $menu_payload = $this->menuGenerator->generateForCurrentUser();
                $menu_items = $menu_payload['items'] ?? [];
            } catch (\Throwable $menuException) {
                // Graceful fallback: just omit menu
            }

            return new WP_REST_Response([
                'success' => true,
                'message' => 'Complete debug structure retrieved',
                'meta' => $route_meta,
                'data' => [
                    'baseConfig' => null,
                    'roleOverrides' => $role_overrides,
                    'completeStructure' => $complete_structure,
                    'version' => \count($role_overrides) > 0 ? 1 : 0,
                    'rawData' => [
                        'completeStructure' => $complete_structure,
                        'roleOverrides' => $role_overrides,
                    ],
                    'menu' => $menu_items,
                    'roles' => \wp_roles()->roles,
                ],
            ], 200);
        } catch (\Throwable $e) {
            return new WP_REST_Response([
                'success' => false,
                'message' => 'Failed to get complete debug structure',
                'meta' => $route_meta,
                'fallback_data' => [
                    'baseConfig' => null,
                    'roleOverrides' => [],
                    'completeStructure' => [],
                    'version' => 0,
                    'rawData' => [
                        'completeStructure' => [],
                        'roleOverrides' => [],
                    ],
                    'menu' => [],
                    'roles' => \wp_roles()->roles,
                ],
            ], 200);
        }
    }
}
