<?php

namespace Daylight\Core\Modules\FormBuilder;

use Daylight\Core\Modules\Admin\Entities\Page;
use Daylight\Core\Modules\Admin\Registries\NavigationRegistry;
use Daylight\Core\Modules\FormBuilder\Http\Controllers\FormController;
use Daylight\Core\Modules\FormBuilder\Livewire\FormBuilder;
use Daylight\Core\Modules\FormBuilder\Livewire\FormRenderer;
use Daylight\Core\Modules\FormBuilder\Livewire\FormSubmissionTable;
use Daylight\Core\Modules\FormBuilder\Livewire\FormTable;
use Daylight\Core\Modules\FormBuilder\Livewire\ResendSubmission;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Livewire\Livewire;

class FormBuilderServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        //
    }

    public function boot(): void
    {
        $this->registerLivewireComponents();
        $this->registerNavigation();
        $this->registerRoutes();
    }

    protected function registerLivewireComponents(): void
    {
        if (! class_exists(Livewire::class)) {
            return;
        }

        Livewire::component('daylight::form-builder', FormBuilder::class);
        Livewire::component('daylight::form-renderer', FormRenderer::class);
        Livewire::component('daylight::form-table', FormTable::class);
        Livewire::component('daylight::form-submission-table', FormSubmissionTable::class);
        Livewire::component('daylight::resend-submission', ResendSubmission::class);
    }

    protected function registerNavigation(): void
    {
        NavigationRegistry::addPage(
            Page::create()
                ->setKey('forms')
                ->setLabel(__('daylight::forms.forms'))
                ->setPermission('read:pages')
                ->setRoute('daylight.content.forms.index')
                ->setOrder(500)
                ->setParent('content')
        );
    }

    protected function registerRoutes(): void
    {
        Route::middleware(['web', 'auth', 'can:view:administration', 'no-cache'])
            ->name('daylight.')
            ->prefix(config('daylight.admin_path', 'admin'))
            ->group(function () {
                Route::prefix('content')
                    ->as('content.')
                    ->group(function () {
                        Route::resource('forms', FormController::class);
                        Route::post('forms/{form}/duplicate', [FormController::class, 'duplicate'])
                            ->name('forms.duplicate');
                        Route::get('forms/{form}/submissions', [FormController::class, 'submissions'])
                            ->name('forms.submissions');
                        Route::get('forms/{form}/submissions/{submission}', [FormController::class, 'showSubmission'])
                            ->name('forms.submissions.show');
                        Route::delete('forms/{form}/submissions/{submission}', [FormController::class, 'destroySubmission'])
                            ->name('forms.submissions.destroy');
                    });
            });
    }
}
