<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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);
    }
}
