<?php

namespace App\Providers\Filament;

use App\Http\Middleware\EnsureProfileIsFilled;
use App\Livewire\UserProfileFields;
use App\Modules\BaseAuth\Middleware\RedirectIfUserPhoneIsUnVerfied;
use BezhanSalleh\FilamentShield\FilamentShieldPlugin;
use CraftForge\FilamentLanguageSwitcher\FilamentLanguageSwitcherPlugin;
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 Filament\Support\Colors\Color;
use Filament\Widgets\AccountWidget;
use Filament\Widgets\FilamentInfoWidget;
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 Joaopaulolndev\FilamentEditProfile\FilamentEditProfilePlugin;

class WorkPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('work')
            ->path(filament_path())
            ->colors([
                'primary' => Color::Amber,
            ])
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources')
            ->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages')
            ->discoverClusters(in: app_path('Filament/Clusters'), for: 'App\\Filament\\Clusters')
            ->pages([
                Dashboard::class,
            ])
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\Filament\Widgets')
            ->widgets([
                AccountWidget::class,
                FilamentInfoWidget::class,
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                RedirectIfUserPhoneIsUnVerfied::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->plugins([
                FilamentShieldPlugin::make()
                    ->navigationGroup('Roles and permissions'),

                FilamentLanguageSwitcherPlugin::make()
                    ->locales([
                        ['code' => 'tk', 'name' => 'Turkmen', 'flag' => 'tm'],
                        ['code' => 'en', 'name' => 'English', 'flag' => 'us'],
                        ['code' => 'ru', 'name' => 'Russian', 'flag' => 'ru'],
                    ]),

                FilamentEditProfilePlugin::make()
                    ->setTitle(__('My profile'))
                    ->setNavigationLabel(__('My profile'))
                    ->setIcon('heroicon-o-user-circle')
                    ->shouldShowEditProfileForm(false)
                    ->customProfileComponents([
                        UserProfileFields::class,
                    ]),
            ])
            ->authMiddleware([
                Authenticate::class,
                EnsureProfileIsFilled::class,
            ])
            ->spa()
            ->databaseTransactions()
            ->colors([
                'danger' => Color::Rose,
                'gray' => Color::Gray,
                'info' => Color::Blue,
                'primary' => Color::Indigo,
                'success' => Color::Emerald,
                'warning' => Color::Orange,
            ]);
    }
}
