<?php

namespace App\Providers\Filament;

use App\Models\User;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\AuthenticateSession;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Navigation\NavigationGroup;
use Filament\Navigation\NavigationItem;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Tables\Columns\Column;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Table;
use Livewire\Livewire;
use NexusPlugin\TelegramBot\Filament\TelegramBotBindsResource;
use NexusPlugin\TelegramBot\Filament\TelegramBotResource;

class AppPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('admin')
            ->homeUrl("/")
            ->sidebarWidth("15rem")
            ->topbar(true)
            ->sidebarCollapsibleOnDesktop(true)
            ->authGuard("nexus-web")
            ->path('nexusphp')
            ->login()
            ->colors([
                'primary' => Color::Amber,
            ])
            ->resources([
//                TelegramBotResource::class,
//                TelegramBotBindsResource::class
            ])
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
            ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
            ->pages([
//                Pages\Dashboard::class,
                \App\Filament\Pages\Dashboard::class,
            ])
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
            ->widgets([
//                Widgets\AccountWidget::class,
//                Widgets\FilamentInfoWidget::class,
            ])
            ->discoverClusters(app_path('Filament/Clusters'), for: 'App\\Filament\\Clusters')
            ->middleware([
//                EncryptCookies::class,
                \App\Http\Middleware\EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
//                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
                \App\Http\Middleware\Locale::class,
            ])
            ->authMiddleware([
                \App\Http\Middleware\Filament::class,
            ])
            ->navigationItems([
                NavigationItem::make('Horizon')
                ->label(fn () => nexus_trans('admin.sidebar.queue_monitor', [], Auth::user() ? get_langfolder_cookie(true) : 'en'))
                ->icon('heroicon-o-presentation-chart-line')
                ->group('System')
                ->sort(99)
                ->url('/horizon')
                ->openUrlInNewTab()
                ->hidden(fn() => !(Auth::user() && Auth::user()->class >= User::CLASS_SYSOP))
            ])
            ;
    }

    public function boot()
    {
        Table::configureUsing(function (Table $table): void {
            $table
                ->filtersLayout(FiltersLayout::AboveContent)
                ->paginationPageOptions([10, 25, 50, 100])
            ;
        });
        Column::configureUsing(function (Column $section): void {
            $section
                ->disabledClick();
        });
    }

    public function register(): void
    {
        parent::register(); // TODO: Change the autogenerated stub
        Livewire::setUpdateRoute(function ($handle) {
            return Route::post('/livewire/update', $handle)->middleware('filament');
        });
    }

}
