<?php

namespace Modules\NX600Lab\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Factory;
use Modules\NX600Lab\Console\Nx600TcpServer;
use Modules\NX600Lab\Console\Nx600Hl7Server;
use Modules\NX600Lab\Console\Nx600Simulator;

class NX600LabServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->registerConfig();
        $this->registerViews();
        $this->registerFactories();
        $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
        $this->registerTranslations();
        $this->registerLabCoreDriver();

        if ($this->app->runningInConsole()) {
            $this->commands([
                Nx600TcpServer::class,
                Nx600Hl7Server::class,
                Nx600Simulator::class,
            ]);
        }
    }

    /**
     * Advertise NX600 to the LabCore engine via its DriverRegistry, so the
     * shared LabCore UI / agent-sync API recognise NX600 devices. Degrades
     * gracefully if the LabCore module is not present.
     */
    protected function registerLabCoreDriver(): void
    {
        if (class_exists(\Modules\LabCore\Entities\Services\DriverRegistry::class)) {
            \Modules\LabCore\Entities\Services\DriverRegistry::register(
                new \Modules\NX600Lab\Entities\Drivers\Nx600Driver()
            );
        }
    }

    public function register()
    {
        $this->app->register(RouteServiceProvider::class);

        // Lab adapter registry (singleton so additional adapter packages can
        // append themselves at boot via the same instance).
        $this->app->singleton(\Modules\NX600Lab\Entities\Services\LabProviderRegistry::class);
    }

    protected function registerConfig()
    {
        $this->publishes([
            __DIR__ . '/../Config/config.php' => config_path('nx600lab.php'),
        ], 'config');
        $this->mergeConfigFrom(
            __DIR__ . '/../Config/config.php', 'nx600lab'
        );
    }

    public function registerViews()
    {
        $viewPath = resource_path('views/modules/NX600Lab');
        $sourcePath = __DIR__ . '/../Resources/views';

        $this->publishes([
            $sourcePath => $viewPath,
        ], 'views');

        $this->loadViewsFrom(array_merge(array_map(function ($path) {
            return $path . '/modules/nx600Lab';
        }, \Config::get('view.paths')), [$sourcePath]), 'nx600Lab');
    }

    public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/NX600Lab');

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, 'nx600Lab');
        } else {
            $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'nx600Lab');
        }
    }

    public function registerFactories()
    {
        if (!app()->environment('production') && $this->app->runningInConsole()) {
            app(Factory::class)->load(__DIR__ . '/../Database/factories');
        }
    }

    public function provides()
    {
        return [];
    }
}
