<?php

namespace App\Providers\Filament;

use App\Filament\Pages\Integrations\KnowledgeGraphExplorer;
use App\Filament\Pages\KnowledgePoints;
use App\Filament\Pages\KnowledgeGraphManagement;
use App\Filament\Pages\MistakeBook;
use App\Filament\Pages\QuestionManagement;
use App\Filament\Pages\PromptManagement;
use App\Filament\Pages\RecommendationList;
use App\Filament\Pages\StudentDashboard;
use App\Filament\Pages\StudentManagement;
use App\Filament\Pages\StudentManagementQuickAccess;
use App\Filament\Pages\TextbookImport\TextbookExcelImportPage;
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;
use Filament\Support\Facades\FilamentView;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('admin')
            ->path('admin')
            ->login(CustomLogin::class)
            ->colors([
                'primary' => Color::hex('#0ea5e9'), // 使用DaisyUI的primary色
                'gray' => Color::hex('#6b7280'),
                'warning' => Color::hex('#f59e0b'),
                'success' => Color::hex('#10b981'),
                'danger' => Color::hex('#ef4444'),
            ])
            ->defaultAvatarProvider(\App\Providers\Filament\AvatarProviders\DiceBearAvatarProvider::class)
            ->darkMode(false) // 默认使用浅色模式，保留DaisyUI主题
            ->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,
                MistakeBook::class,
                StudentManagementQuickAccess::class,
                KnowledgePoints::class,
                KnowledgeGraphManagement::class,
                QuestionManagement::class,
                PromptManagement::class,
                RecommendationList::class,
                KnowledgeGraphExplorer::class,
                TextbookExcelImportPage::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())
            ->renderHook(
                'panels::page.header.actions.after',
                fn () => view('filament.components.menu-visibility-toggle')
            )
            ->navigationGroups([
                '教材管理',
                '卷子导入流程',
                '知识图谱管理',
                '题库管理',
                '卷子生成管理',
                '学生管理',
                'API 管理',
                '其他',
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ]);
    }
}
