<?php

namespace App\Filament\Tenant\Pages;

use Filament\Actions;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;

class ApiTokens extends Page
{
    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-key';

    protected static ?string $navigationLabel = 'API Tokens';

    protected static string|\UnitEnum|null $navigationGroup = 'Admin';

    protected static ?int $navigationSort = 95;

    protected static ?string $title = 'API Tokens';

    protected string $view = 'filament.tenant.pages.api-tokens';

    public ?string $newToken = null;

    public function getTokens(): array
    {
        $u = auth()->user();
        if (! $u) return [];
        return $u->tokens()->latest()->get()->map(fn ($t) => [
            'id' => $t->id,
            'name' => $t->name,
            'last_used_at' => $t->last_used_at?->diffForHumans() ?? '—',
            'created_at' => $t->created_at->diffForHumans(),
            'abilities' => is_array($t->abilities) ? implode(', ', $t->abilities) : '*',
        ])->all();
    }

    protected function getHeaderActions(): array
    {
        return [
            Actions\Action::make('create')
                ->label('Generează token')
                ->icon('heroicon-m-plus')
                ->schema([
                    Forms\Components\TextInput::make('name')
                        ->label('Nume (descriere)')
                        ->required()
                        ->placeholder('ex: app-mobil, integrare-erp'),
                ])
                ->action(function (array $data) {
                    $u = auth()->user();
                    if (! $u) return;
                    $token = $u->createToken($data['name']);
                    $this->newToken = $token->plainTextToken;
                    Notification::make()
                        ->title('Token generat!')
                        ->body('Copiază token-ul ACUM — nu va mai fi afișat.')
                        ->success()
                        ->persistent()
                        ->send();
                }),
        ];
    }

    public function deleteToken(int $id): void
    {
        $u = auth()->user();
        if (! $u) return;
        $u->tokens()->where('id', $id)->delete();
        Notification::make()->title('Token revocat')->success()->send();
    }

    public function dismissNew(): void
    {
        $this->newToken = null;
    }
}
