<?php

namespace Formatrica\REST;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

use Formatrica\Setup\Service_Locator;
use Formatrica\Setup\Service_Provider;

/**
 * Free REST provider — statically Free-only (Phase 6, plan §6.8). It neither
 * names nor conditionally instantiates Pro controllers; the Pro service
 * provider adds its controllers (AI, payments, drafts, license, submission
 * preview) to Rest_Controller_Registry, resolved lazily at rest_api_init.
 */
final class Rest_Service_Provider implements Service_Provider
{
    /**
     * @var array<int, Rest_Controller>
     */
    private array $controllers;

    public function register(Service_Locator $services): void
    {
        $this->controllers = [
            new Form_Controller(),
            new Submission_Controller(),
            new Utility_Controller(),
            new Integration_Controller(),
            new Settings_Controller(),
        ];

        add_action('rest_api_init', [$this, 'register_routes']);

        foreach ($this->controllers as $controller) {
            $services->set(get_class($controller), $controller);
        }
    }

    public function register_routes(): void
    {
        foreach ($this->controllers as $controller) {
            $controller->register_routes();
        }

        // Edition extensions (Pro controllers), registered by their provider.
        foreach (Rest_Controller_Registry::controllers() as $controller) {
            $controller->register_routes();
        }
    }
}
