<?php

namespace Modules\ClinicLab\Providers;

use Illuminate\Support\ServiceProvider;

/**
 * Thin bridge provider connecting ClinicCore patients to LabCore lab orders and
 * results. Registers its own views + migrations only; all UI lives under
 * /clinic-lab so no ClinicCore or LabCore files are modified.
 */
class ClinicLabServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->registerConfig();
        $this->registerTranslations();
        $this->registerViews();
        $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
    }

    public function register()
    {
        //
    }

    /**
     * Merge the module config under the `cliniclab` key so the InstallController
     * can read config('cliniclab.name') / config('cliniclab.module_version'),
     * mirroring the LabCore convention.
     */
    public function registerConfig()
    {
        $this->mergeConfigFrom(__DIR__ . '/../Config/config.php', 'cliniclab');
    }

    /**
     * Register the `clinicLab::lang.*` namespace used by the DataController menu
     * / permission labels and the P4-T4 views. Mirrors the LabCore convention.
     */
    public function registerTranslations()
    {
        $langPath = resource_path('lang/modules/ClinicLab');

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

    public function registerViews()
    {
        $sourcePath = __DIR__ . '/../Resources/views';
        $this->loadViewsFrom(array_merge(array_map(function ($path) {
            return $path . '/modules/clinicLab';
        }, \Config::get('view.paths')), [$sourcePath]), 'clinicLab');
    }
}
