<?php

namespace App\Providers\Filament;

use App\Filament\Resources\PluginResource;
use App\Http\Middleware\SetLocale;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Navigation\MenuItem;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
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\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\HtmlString;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        $panel = $panel
            ->default()
            ->id('admin')
            ->path('admin')
            ->login()
            ->brandLogo(fn () => new HtmlString(
                '<img src="'.asset('icon.png').'" alt="App Icon" style="height: 2.5rem; display: inline-block; vertical-align: middle; margin-right: 0.5rem;" />'.
                '<span style="vertical-align: middle; font-weight: bold; font-size: 1.25rem;">'.config('app.name').'</span>'
            ))
            ->colors([
                'primary' => Color::Amber,
            ])
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
            ->resources([
                // PluginResource::class, // Removed as it's a custom page now
            ])
            ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
            ->pages([
                Pages\Dashboard::class,
                \App\Filament\Pages\InstallPluginPage::class,
                // \App\Filament\Pages\ListPlugins::class, // Removed duplicate entry
            ])
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
            ->widgets([
                Widgets\AccountWidget::class,
                Widgets\FilamentInfoWidget::class,
                \App\Filament\Widgets\ApiProviderHealth::class,
                \App\Filament\Widgets\AppStatsOverview::class,
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                SetLocale::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ])
            ->plugins([

            ])
            ->profile(isSimple: false)
            ->userMenuItems([
                MenuItem::make()
                    ->label(fn () => 'English')
                    ->color(fn () => App::getLocale() === 'en' ? 'primary' : null)
                    ->icon('heroicon-o-language')
                    ->url(fn () => request()->fullUrlWithQuery(['locale' => 'en'])),
                MenuItem::make()
                    ->label(fn () => 'Deutsch')
                    ->color(fn () => App::getLocale() === 'de' ? 'primary' : null)
                    ->icon('heroicon-o-language')
                    ->url(fn () => request()->fullUrlWithQuery(['locale' => 'de'])),
            ]);

        if (Auth::check()) {
            $user = Auth::user();
            if ($user->theme_preference === 'dark') {
                $panel->darkMode();
            } else {
                $panel->lightMode();
            }
        }

        return $panel;
    }
}
