<?php

namespace App\Providers\Filament;

use App\Models\Attendance;
use App\Models\BusinessTrip;
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\MenuItem;
use Filament\Navigation\NavigationBuilder;
use Filament\Navigation\NavigationGroup;
use Filament\Navigation\NavigationItem;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Support\Enums\MaxWidth;
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\Blade;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('admin')
            ->path('admin')
            ->login()
            ->profile()
            ->brandName('Nizhamia')
            ->brandLogo(fn () => view('filament.components.brand-logo'))
            ->darkModeBrandLogo(fn () => view('filament.components.brand-logo-dark'))
            ->brandLogoHeight('2.5rem')
            ->favicon(asset('favicon.ico'))
            ->darkMode(true)
            ->colors([
                'primary' => Color::hex('#B56DA0'), // Nizhamia Magenta - primary brand color
                'danger' => Color::Rose,
                'gray' => Color::Slate,
                'info' => Color::hex('#5BA8B5'), // Nizhamia Teal - secondary brand color
                'success' => Color::Emerald,
                'warning' => Color::hex('#E8D24A'), // Nizhamia Gold - tertiary brand color
            ])
            ->font('Inter')
            ->maxContentWidth(MaxWidth::SevenExtraLarge)
            ->sidebarCollapsibleOnDesktop()
            ->sidebarWidth('18rem')
            ->collapsedSidebarWidth('5rem')
            ->sidebarFullyCollapsibleOnDesktop()
            ->navigationGroups([
                NavigationGroup::make()
                    ->label('Dashboard')
                    ->icon('heroicon-o-squares-2x2')
                    ->collapsed(false),
                NavigationGroup::make()
                    ->label('Kehadiran')
                    ->icon('heroicon-o-clock')
                    ->collapsed(false),
                NavigationGroup::make()
                    ->label('Perjalanan Dinas')
                    ->icon('heroicon-o-paper-airplane')
                    ->collapsed(false),
                NavigationGroup::make()
                    ->label('Approval')
                    ->icon('heroicon-o-check-circle')
                    ->collapsed(false),
                NavigationGroup::make()
                    ->label('Manajemen User')
                    ->icon('heroicon-o-user-group')
                    ->collapsed(true),
                NavigationGroup::make()
                    ->label('Master Data')
                    ->icon('heroicon-o-circle-stack')
                    ->collapsed(true),
                NavigationGroup::make()
                    ->label('Pengaturan')
                    ->icon('heroicon-o-cog-8-tooth')
                    ->collapsed(true),
            ])
            ->userMenuItems([
                'profile' => MenuItem::make()
                    ->label('Profil Saya')
                    ->icon('heroicon-o-user-circle')
                    ->url(fn (): string => route('filament.admin.pages.dashboard')),
                MenuItem::make()
                    ->label('Pengaturan')
                    ->icon('heroicon-o-cog-6-tooth')
                    ->url(fn (): string => route('filament.admin.pages.dashboard')),
                'logout' => MenuItem::make()
                    ->label('Keluar')
                    ->icon('heroicon-o-arrow-left-on-rectangle'),
            ])
            ->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\ExecutiveDashboard::class,
                \App\Filament\Pages\HRDDashboard::class,
            ])
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
            ->widgets([
                \App\Filament\Widgets\StatsOverview::class,
                \App\Filament\Widgets\RecentBusinessTripsWidget::class,
                \App\Filament\Widgets\AttendanceChartWidget::class,
                \App\Filament\Widgets\PendingApprovalsWidget::class,
                \App\Filament\Widgets\RecentActivityWidget::class,
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ])
            ->databaseNotifications()
            ->databaseNotificationsPolling('30s')
            ->globalSearchKeyBindings(['command+k', 'ctrl+k'])
            ->globalSearchFieldKeyBindingSuffix()
            ->breadcrumbs(true)
            ->topNavigation(false)
            ->spa()
            // Disable browser native unsaved changes alert
            // Custom modal will be handled via JavaScript
            // ->unsavedChangesAlerts()
            ->renderHook(
                'panels::scripts.after',
                fn () => view('filament.scripts.unsaved-changes-modal'),
            );
            // ->viteTheme('resources/css/filament-custom.css');
            // ->renderHook(
            //     'panels::body.end',
            //     fn () => view('filament.footer'),
            // )
            // ->renderHook(
            //     'panels::body.end',
            //     fn () => view('filament.components.quick-actions'),
            // )
            // ->renderHook(
            //     'panels::sidebar.nav.start',
            //     fn () => view('filament.components.sidebar-header'),
            // )
            // ->renderHook(
            //     'panels::content.start',
            //     fn () => view('filament.components.topbar', [
            //         'greeting' => $this->getGreeting(),
            //         'todayAttendance' => Attendance::whereDate('date', today())->count(),
            //         'pendingApprovals' => User::where('approval_status', 'pending')->count() + BusinessTrip::where('status', 'pending')->count(),
            //         'ongoingTrips' => BusinessTrip::where('status', 'ongoing')->count(),
            //     ]),
            // )
            // ->viteTheme('resources/css/filament-custom.css');
    }

    protected function getGreeting(): string
    {
        $hour = now()->hour;

        if ($hour < 12) {
            return 'Selamat Pagi,';
        } elseif ($hour < 15) {
            return 'Selamat Siang,';
        } elseif ($hour < 18) {
            return 'Selamat Sore,';
        } else {
            return 'Selamat Malam,';
        }
    }
}
