<?php

declare(strict_types=1);

namespace App\Filament\Pages;

use App\Models\User;
use App\Services\DberpAdminMenuShortcutService;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use UnitEnum;

final class DberpCommonShortcutsPreferencesPage extends Page
{
    protected static ?string $slug = 'common-shortcuts-preferences';

    protected static ?int $navigationSort = 1;

    protected string $view = 'filament.pages.dberp-common-shortcuts-preferences';

    public static function getNavigationGroup(): string|UnitEnum|null
    {
        return __('dberp.nav.common_functions');
    }

    public static function getNavigationLabel(): string
    {
        return __('dberp.shortcuts.preferences.nav_label');
    }

    public static function getNavigationIcon(): string|BackedEnum|Htmlable|null
    {
        return null;
    }

    public static function canAccess(): bool
    {
        return Auth::check();
    }

    public function getTitle(): string|Htmlable
    {
        return __('dberp.shortcuts.preferences.page_title');
    }

    public function getSubheading(): string|Htmlable|null
    {
        return __('dberp.shortcuts.preferences.intro');
    }

    /**
     * @return array<Action>
     */
    protected function getHeaderActions(): array
    {
        return [
            Action::make('clearPinned')
                ->label(__('dberp.shortcuts.preferences.clear_all'))
                ->color('gray')
                ->visible(fn (): bool => app(DberpAdminMenuShortcutService::class)->hasPinnedShortcuts(Auth::user()))
                ->requiresConfirmation()
                ->modalHeading(__('dberp.shortcuts.preferences.clear_confirm_heading'))
                ->modalDescription(__('dberp.shortcuts.preferences.clear_confirm_body'))
                ->action(function (): void {
                    $user = Auth::user();
                    if (! $user instanceof User) {
                        return;
                    }
                    app(DberpAdminMenuShortcutService::class)->clearPinnedShortcuts($user);
                    Notification::make()
                        ->title(__('dberp.shortcuts.preferences.clear_done'))
                        ->success()
                        ->send();
                }),
        ];
    }

    public function pinShortcut(string $shortcutKey): void
    {
        $user = Auth::user();
        if (! $user instanceof User) {
            return;
        }

        try {
            app(DberpAdminMenuShortcutService::class)->pin($user, $shortcutKey);
            Notification::make()
                ->title(__('dberp.shortcuts.preferences.pinned'))
                ->success()
                ->send();
        } catch (ValidationException $exception) {
            Notification::make()
                ->title(__('dberp.shortcuts.preferences.pin_failed'))
                ->danger()
                ->body(collect($exception->errors())->flatten()->first())
                ->send();
        }
    }

    public function unpinShortcut(int $recordId): void
    {
        $user = Auth::user();
        if (! $user instanceof User) {
            return;
        }

        app(DberpAdminMenuShortcutService::class)->unpin($user, $recordId);
        Notification::make()
            ->title(__('dberp.shortcuts.preferences.unpinned'))
            ->success()
            ->send();
    }

    public function moveShortcutUp(int $recordId): void
    {
        $user = Auth::user();
        if (! $user instanceof User) {
            return;
        }

        app(DberpAdminMenuShortcutService::class)->moveUp($user, $recordId);
    }

    public function moveShortcutDown(int $recordId): void
    {
        $user = Auth::user();
        if (! $user instanceof User) {
            return;
        }

        app(DberpAdminMenuShortcutService::class)->moveDown($user, $recordId);
    }
}
