<?php

namespace Modules\DermatologyClinic\Providers;

use Illuminate\Support\ServiceProvider;

/**
 * Thin specialty-plugin provider. DermatologyClinic is now a ClinicCore
 * plugin: it contributes a dermatology driver, a 1:1 patient extension table
 * (derma_patient_profiles) and the Blade fragments ClinicCore injects into its
 * shared patient screens. All clinic UI/workflow lives in ClinicCore.
 */
class DermatologyClinicServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->registerViews();
        $this->registerTranslations();
        $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
        $this->registerSpecialtyDriver();
    }

    public function register()
    {
        //
    }

    /** Register the dermaClinic:: view namespace so ClinicCore can resolve our fragments. */
    public function registerViews()
    {
        $sourcePath = __DIR__ . '/../Resources/views';
        $this->loadViewsFrom(array_merge(array_map(function ($path) {
            return $path . '/modules/dermaClinic';
        }, \Config::get('view.paths')), [$sourcePath]), 'dermaClinic');
    }

    /** Register the dermaClinic:: translation namespace (permission labels + fragment UI). */
    public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/dermaClinic');
        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, 'dermaClinic');
        } else {
            $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'dermaClinic');
        }
    }

    /**
     * Register Dermatology as a structural specialty with ClinicCore's
     * SpecialtyDriverRegistry. Degrades gracefully if ClinicCore is absent.
     */
    protected function registerSpecialtyDriver(): void
    {
        try {
            if (class_exists(\Modules\ClinicCore\Entities\Services\SpecialtyDriverRegistry::class)) {
                \Modules\ClinicCore\Entities\Services\SpecialtyDriverRegistry::register(
                    new \Modules\DermatologyClinic\Entities\Drivers\DermatologySpecialtyDriver()
                );
            }
        } catch (\Throwable $e) {
            // Never let driver wiring break boot().
        }
    }
}
