<?php
/**
 * REST API Tools Controller.
 *
 * @package NimblyMail\SMTP
 */

namespace NimblyMail\SMTP\Rest;

use NimblyMail\SMTP\ToolsProvider;
use WP_REST_Server;
use WP_REST_Response;

defined( 'ABSPATH' ) || exit;

/**
 * Class ToolsController
 *
 * Exposes the registered tools list over the REST API so the Vue
 * Tools SPA can render tabs dynamically.
 *
 * @since 1.0.0
 */
class ToolsController extends AbstractController {

    /**
     * Register the REST routes.
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function register_routes(): void {
        register_rest_route(
            self::NAMESPACE,
            '/tools',
            array(
                array(
                    'methods'             => WP_REST_Server::READABLE,
                    'callback'            => array( $this, 'get_tools' ),
                    'permission_callback' => array( $this, 'permissions_check' ),
                ),
            )
        );
    }

    /**
     * Get the list of registered tools.
     *
     * @since 1.0.0
     *
     * @return WP_REST_Response
     */
    public function get_tools(): WP_REST_Response {
        return new WP_REST_Response(
            array(
                'tools' => ToolsProvider::get_instance()->get_tools(),
            )
        );
    }
}
