<?php

namespace App\Providers;

use Setting;
use Mtc\Shop\Item;
use App\TwigHelper;
use Mtc\Shop\Category;
use Mtc\Shop\Item\Size;
use Mtc\Shop\Item\Image;
use App\Services\ComponentScannerService;
use App\Services\ComponentRendererService;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Relations\Relation;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register()
    {
        // Register Component Registry services
        $this->app->singleton(ComponentScannerService::class);
        $this->app->singleton(ComponentRendererService::class);
    }

    public function boot()
    {
        $this->optimizes(clear: 'twig:clear', key: 'twig');

        if (!defined('SITE_PATH')) {
            define('SITE_PATH', base_path());
        }

        require_once base_path('sites/sites.php');
        require_once base_path('sites/loader.php');
        require_once base_path('core/includes/settings.php');

        if (Schema::hasTable('settings')) {
            Setting::import();
            Setting::loadAndDefine();
        }

        if (php_sapi_name() === 'cli') {
            require_once SITE_PATH . '/shop/browse/classes/productfilter.class.php';
            require_once SITE_PATH . '/core/includes/twig.php';

            TwigHelper::addCustomFunctions();
        }

        // $this->configureListeners();

        Relation::morphMap([
            'item' => Item::class,
            'item_size' => Size::class,
            'category' => Category::class,
            'item_image' => Image::class,
        ]);

        if (config('app.debug')) {
            if (php_sapi_name() != "cli") {
                //$this->app->bind(ExceptionRenderer::class, WhoopsExceptionRenderer::class);
            }
        }
    }
}
