<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Api\Plugins\PluginLoader;
use App\Api\Plugins\ApiPluginInterface;
use Illuminate\Support\Facades\File;

class ApiPluginServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        $path = app_path('Api/Plugins');
        $files = File::files($path);

        foreach ($files as $file) {
            $filename = $file->getFilenameWithoutExtension();
            if (in_array($filename, ['ApiPluginInterface', 'PluginLoader'])) {
                continue;
            }

            $class = 'App\\Api\\Plugins\\' . $filename;
            if (class_exists($class) && in_array(ApiPluginInterface::class, class_implements($class))) {
                PluginLoader::registerPlugin($filename, $class);
            }
        }
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        //
    }
}