<?php

namespace App\Providers\Filament;

use App\Filament\Pages\Integrations\KnowledgeGraphExplorer;
use App\Filament\Pages\Integrations\KnowledgeGraphIntegration;
use App\Filament\Pages\KnowledgePoints;
use App\Filament\Pages\KnowledgeMindmap;
use App\Filament\Pages\QuestionManagement;
use App\Filament\Pages\PromptManagement;
use App\Filament\Pages\Statistics\KnowledgePointStats;
use App\Filament\Pages\StudentDashboard;
use App\Filament\Pages\StudentManagement;
use App\Filament\Pages\StudentKnowledgeGraphPage;
use App\Filament\Widgets\DashboardQuickLinks;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\AuthenticateSession;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Pages\Dashboard;
use Filament\Panel;
use Filament\PanelProvider;
use App\Filament\Auth\Pages\CustomLogin;
use Filament\Support\Colors\Color;
use Filament\Widgets\AccountWidget;
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\View\Middleware\ShareErrorsFromSession;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('admin')
            ->path('admin')
            ->login(CustomLogin::class)
            ->colors([
                'primary' => Color::hex('#4163ff'),
            ])
            ->defaultAvatarProvider(\App\Providers\Filament\AvatarProviders\DiceBearAvatarProvider::class)
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources')
            ->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages')
            ->pages([
                Dashboard::class,
                StudentManagement::class,
                StudentDashboard::class,
                StudentKnowledgeGraphPage::class,
                KnowledgePoints::class,
                KnowledgeMindmap::class,
                QuestionManagement::class,
                PromptManagement::class,
                KnowledgePointStats::class,
                KnowledgeGraphIntegration::class,
                KnowledgeGraphExplorer::class,
            ])
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\Filament\Widgets')
            ->widgets([
                DashboardQuickLinks::class,
                AccountWidget::class,
            ])
            ->renderHook('panels::head.end', fn (): string =>
                view('filament.layout.vite-styles')->render() . view('filament.layout.vite-scripts')->render()
            )
            ->renderHook('global::head.start', fn (): string => view('filament.layout.vite-styles')->render())
            ->renderHook('panels::body.end', fn (): string => view('filament.layout.math-renderer')->render())
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ]);
    }
}
