<?php

namespace Licenzo\ServiceProvider;

use Licenzo\Framework\Foundation\ServiceProvider;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class RouteServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('router', function ($app) {
            return new \Licenzo\Framework\Http\Router\Router($app);
        });

        $this->app->alias('router', \Licenzo\Framework\Contracts\Router\Router::class);
        do_action('licenzo_router_loaded', $this->app, $this->app->make('router'));
    }

    public function boot()
    {
        add_action('rest_api_init', function () {
            $router = $this->app->make('router');
            do_action('licenzo_routes_loading', $this->app, $router);
            require $this->app->basePath('src/routes/api.php');
            do_action('licenzo_routes_loaded', $this->app, $router);
            $router->dispatch();
        });
    }
}
