<?php

declare(strict_types=1);

namespace Numinix\SeekmodoWordPress\Rest;

// Parent class that owns the `/wp-json/seekmodo/v1/*` namespace.
// Individual route handlers (Typeahead, ClickBeacon, ConnectFlow,
// Indexer) register their own routes from their own `register()`s.
// This class exists to centralize the namespace constant + the health
// endpoint.

final class Routes
{
    public const NAMESPACE_NAME = 'seekmodo/v1';

    public static function register(): void
    {
        if (!function_exists('add_action')) {
            return;
        }
        add_action('rest_api_init', [self::class, 'registerHealthRoute']);
    }

    public static function registerHealthRoute(): void
    {
        if (!function_exists('register_rest_route')) {
            return;
        }
        register_rest_route(self::NAMESPACE_NAME, '/health', [
            'methods'             => 'GET',
            'callback'            => [self::class, 'health'],
            'permission_callback' => static function (): bool {
                return function_exists('current_user_can') ? current_user_can('manage_options') : false;
            },
        ]);
    }

    /**
     * @return array<string,mixed>
     */
    public static function health(): array
    {
        return [
            'version'       => SEEKMODO_WP_VERSION,
            'paired'        => \Numinix\SeekmodoWordPress\Config\Options::isPaired(),
            'tenant_id'     => \Numinix\SeekmodoWordPress\Config\Options::tenantId(),
            'mode'          => \Numinix\SeekmodoWordPress\Mode\ModeFsm::current(),
            'effective_mode'=> \Numinix\SeekmodoWordPress\Mode\AutoPromoter::effectiveMode(),
            'breaker'       => \Numinix\SeekmodoWordPress\Gateway\CircuitBreaker::state(),
            'storefront_host' => \Numinix\SeekmodoWordPress\Config\DomainLock::currentHost(),
            'metrics'       => \Numinix\SeekmodoWordPress\Mode\AutoPromoter::metrics(),
            'events_queue'  => \Numinix\SeekmodoWordPress\Events\EventsQueue::count(),
        ];
    }
}
