<?php

namespace Lucent;

use Illuminate\Foundation\Application;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService;
use Lucent\Commands\CompileSchemas;
use Lucent\Commands\EmptyTrash;
use Lucent\Commands\GenerateCollectionSchema;
use Lucent\Commands\GenerateFileSchema;
use Lucent\Commands\RebuildThumbnails;
use Lucent\Commands\RemoveOrphanEdges;
use Lucent\Commands\RemoveOrphanRecords;
use Lucent\Commands\Setup;
use Lucent\Commands\SetupDatabase;
use Lucent\Commands\UpgradeFiles122;
use Lucent\File\FileService;
use Lucent\Hook\HookService;
use Lucent\Query\DatabaseGraph\DatabaseGraph;
use Lucent\Query\DatabaseGraph\PgsqlDatabaseGraph;
use Lucent\Query\DatabaseGraph\SqliteDatabaseGraph;
use Lucent\Record\Event\RecordCreated;
use Lucent\Record\Event\RecordUpdated;
use Lucent\Record\Record;
use Lucent\Revision\Listener\CreateRevision;

class LucentServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->singleton(ChannelService::class, function (Application $application) {
            return ChannelService::fromConfig();
        });

        $this->app->singleton(HookService::class, function (Application $application) {
            return new HookService;
        });

        $this->app->bind(ImageManager::class, function () {
            return new ImageManager(Driver::class);
        });

        $this->mergeConfigFrom(
            __DIR__ . '/../config/lucent.php',
            'lucent'
        );


        $this->app->bind(DatabaseGraph::class, function () {
            $dbConnection = config("lucent.database");
            return match (config("database.connections.$dbConnection.driver")) {
                "sqlite" => new SqliteDatabaseGraph(),
                "pgsql" => new PgsqlDatabaseGraph(),
            };
        });
    }

    /**
     * Bootstrap any package services.
     */
    public function boot(Router $router): void
    {





        $manifestPath = public_path('vendor/lucent/dist/manifest.json');
        $manifest = null;
        if (file_exists($manifestPath)) {
            $manifest = json_decode(file_get_contents(public_path('vendor/lucent/dist/manifest.json')), true);
        }

        $router->aliasMiddleware('lucent.auth', \Lucent\Http\Middleware\AuthMiddleware::class);
        $router->aliasMiddleware('lucent.guest', \Lucent\Http\Middleware\GuestMiddleware::class);

        $this->loadViewsFrom(__DIR__ . '/../front/views', 'lucent');
        $this->loadRoutesFrom(__DIR__ . '/Http/web.php');
        $this->loadRoutesFrom(__DIR__ . '/Http/api.php');


        if ($this->app->runningInConsole()) {
            $this->commands([
                Setup::class,
                CompileSchemas::class,
                RebuildThumbnails::class,
                RemoveOrphanEdges::class,
                RemoveOrphanRecords::class,
                SetupDatabase::class,
                GenerateCollectionSchema::class,
                GenerateFileSchema::class,
                UpgradeFiles122::class,
                EmptyTrash::class,
            ]);
        }

        View::share('manifest', $manifest);
        View::share('file', app()->make(FileService::class));
        Blade::anonymousComponentPath(__DIR__ . '../front/views/components', "lucent");

        $this->publishes([
            __DIR__ . '/../config/lucent.php' => config_path('lucent.php'),
        ], "lucent-config");

        $this->publishes([
            __DIR__ . '/../front/dist' => public_path('vendor/lucent/dist'),
            __DIR__ . '/../front/public' => public_path('vendor/lucent/public'),
        ], 'lucent');


        Event::listen(RecordCreated::class,CreateRevision::class);
        Event::listen(RecordUpdated::class,CreateRevision::class);
    }
}
