<?php

namespace Formatrica\Capability;

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

use Formatrica\License\Null_License_Manager;
use Formatrica\Setup\Service_Locator;
use Formatrica\Setup\Service_Provider;

/**
 * Registers the capability + license foundation.
 *
 * Must run before Admin/REST/Frontend providers so every later service can
 * resolve the Feature_Gate from the locator.
 *
 * Free packages do not include app/Pro, so the defensive Pro hook is a no-op
 * there. When the Pro package is installed (same plugin slug), its service
 * provider registers a license-aware capability provider and a real license
 * manager onto the already-registered gate.
 */
final class Capability_Service_Provider implements Service_Provider
{
    public function register(Service_Locator $services): void
    {
        $license = new Null_License_Manager();
        $services->set('license_manager', $license);

        $gate = new Feature_Gate($license);
        $gate->add_provider(new Free_Capability_Provider());
        $services->set('feature_gate', $gate);

        if (class_exists(\Formatrica\Pro\Pro_Service_Provider::class)) {
            (new \Formatrica\Pro\Pro_Service_Provider())->register($services);
        }

        // Expose the fully-merged (Pro-aware) gate to services constructed
        // outside the locator, such as Premium_Save_Guard in Form_Repository.
        Feature_Gate::set_default($gate);
    }
}
