<?php

namespace Modules\Main\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Livewire\Livewire;
use Illuminate\Support\Facades\Gate;

use Modules\Main\Models\Design;

class ModuleServiceProvider extends ServiceProvider
{
    protected string $moduleName = 'Main';

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

    public function boot()
    {
        $this->registerPolicies();
        $this->registerViews();
        $this->registerLivewireViews();
        $this->registerMigrations();
        $this->registerConfig();
        $this->registerComponent();
        $this->registerLivewire();
        $this->registerHelpers();
        try {
            if (!defined('DESIGN_PARAMETERS')) {
                define('DESIGN_PARAMETERS', Design::getParameters());
            }
            if (array_key_exists('title', DESIGN_PARAMETERS)) {
                \Config::set('app.name', DESIGN_PARAMETERS['title']);
            }
        } catch (\Exception $e) {
            if (!defined('DESIGN_PARAMETERS')) {
                define('DESIGN_PARAMETERS', []);
            }
        }
        ;
    }

    protected function registerViews()
    {
        $moduleViewsPath = __DIR__ . '/../Views';
        $this->loadViewsFrom(
            $moduleViewsPath,
            strtolower($this->moduleName)
        );
    }

    protected function registerLivewireViews()
    {
        $moduleViewsPath = __DIR__ . '/../Views/livewire';
        $this->loadViewsFrom(
            $moduleViewsPath,
            strtolower($this->moduleName)
        );
    }

    protected function registerMigrations()
    {
        $this->loadMigrationsFrom(
            app_path('Modules/' . $this->moduleName . '/Database/Migrations')
        );
    }

    protected function registerConfig()
    {
        $path = app_path('Modules/' . $this->moduleName . '/Config/config.php');
        $this->mergeConfigFrom(
            $path,
            strtolower($this->moduleName)
        );
    }

    protected function registerLivewire()
    {
        //Livewire::component('<name>', \Modules\<NAME>\Http\Livewire\<NAME>::class);
        Livewire::component('company.agent.create', \Modules\Main\Http\Livewire\CreateAgentLivewire::class);
        Livewire::component('company.admin.create', \Modules\Main\Http\Livewire\CreateCompanyAdminLivewire::class);
        Livewire::component('clients.search.input', \Modules\Main\Http\Livewire\ClientSearchInputLivewire::class);
    }

    protected function registerComponent()
    {
        //Blade::component('<name>', \Modules\<NAME>\Http\Components\<NAME>::class);
    }

    protected function registerPolicies()
    {
        Gate::policy(\Modules\Main\Models\Company\Company::class, \Modules\Main\Http\Policies\CompanyPolicy::class);
        Gate::policy(\Modules\Main\Models\City::class, \Modules\Main\Http\Policies\ContentPolicy::class);
    }

    protected function registerHelpers()
    {
        $files = glob(__DIR__ . '/../Helpers/' . "*.php");
        foreach ($files as $key => $file) {
            require_once $file;
        }
    }
}