<?php

namespace Mtc\Plugins\BlockManager\Tests\Feature;

use Illuminate\Support\Facades\Route;
use Mtc\Plugins\BlockManager\Providers\BlockManagerServiceProvider;
use Tests\TestCase;

class ServiceProviderTest extends TestCase
{
    public function test_provider_is_registered(): void
    {
        $provider = app()->getProvider(BlockManagerServiceProvider::class);

        $this->assertNotNull(
            $provider,
            'BlockManagerServiceProvider is not registered — add it to the providers array in config/app.php.'
        );

        $this->assertInstanceOf(BlockManagerServiceProvider::class, $provider);
    }

    public function test_provider_loads_routes_file(): void
    {
        $found = collect(Route::getRoutes())->contains(
            fn ($route) => $route->uri() === 'api/block-manager/ping'
        );

        $this->assertTrue(
            $found,
            'Route api/block-manager/ping is not registered — '
            . 'BlockManagerServiceProvider::boot() should loadRoutesFrom(__DIR__.\'/../routes/api.php\').'
        );
    }

    public function test_provider_loads_migrations_dir(): void
    {
        $expected = realpath(__DIR__ . '/../../database/migrations');
        $this->assertNotFalse(
            $expected,
            'plugins/BlockManager/database/migrations directory does not exist.'
        );

        $registered = array_map('realpath', app('migrator')->paths());

        $this->assertContains(
            $expected,
            $registered,
            'BlockManager migrations directory is not registered with the migrator — '
            . 'BlockManagerServiceProvider::boot() should loadMigrationsFrom(__DIR__.\'/../database/migrations\').'
        );
    }
}
