<?php

namespace Botble\{Module}\Providers;

use Botble\{Module}\Models\{Name};
use Illuminate\Support\ServiceProvider;
use Botble\{Module}\Repositories\Caches\{Name}CacheDecorator;
use Botble\{Module}\Repositories\Eloquent\{Name}Repository;
use Botble\{Module}\Repositories\Interfaces\{Name}Interface;
use Botble\Base\Supports\Helper;
use Illuminate\Support\Facades\Event;
use Botble\Base\Traits\LoadAndPublishDataTrait;
use Illuminate\Routing\Events\RouteMatched;

class {Module}ServiceProvider extends ServiceProvider
{
    use LoadAndPublishDataTrait;

    public function register()
    {
        $this->app->bind({Name}Interface::class, function () {
            return new {Name}CacheDecorator(new {Name}Repository(new {Name}));
        });

        Helper::autoload(__DIR__ . '/../../helpers');
    }

    public function boot()
    {
        $this->setNamespace('{types}/{-name}')
            ->loadAndPublishConfigurations(['permissions'])
            ->loadMigrations()
            ->loadAndPublishTranslations()
            ->loadAndPublishViews()
            ->loadRoutes(['web']);

        Event::listen(RouteMatched::class, function () {
            if (defined('LANGUAGE_MODULE_SCREEN_NAME')) {
                \Language::registerModule([{Name}::class]);
            }

            dashboard_menu()->registerItem([
                'id'          => 'cms-{types}-{-name}',
                'priority'    => 5,
                'parent_id'   => null,
                'name'        => '{types}/{-module}::{-name}.name',
                'icon'        => 'fa fa-list',
                'url'         => route('{-name}.index'),
                'permissions' => ['{-name}.index'],
            ]);
        });
    }
}
