<?php

namespace Modules\LabCore\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Factory;

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

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

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

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

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

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

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

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

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

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