<?php

namespace App\Filament\Pages;

use App\Models\Gallery;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use UnitEnum;

class SparkboothConnections extends Page implements HasTable
{
    use InteractsWithTable;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-link';

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

    protected static ?int $navigationSort = 11;

    protected static ?string $title = 'Sparkbooth Verbindungen';

    protected ?string $heading = 'Sparkbooth Verbindungen';

    protected string $view = 'filament.pages.sparkbooth-connections';

    public function table(Table $table): Table
    {
        return $table
            ->heading('Vorhandene Sparkbooth-Verbindungen')
            ->query(
                Gallery::query()
                    ->whereNotNull('upload_token_hash')
                    ->orderByDesc('created_at')
            )
            ->columns([
                TextColumn::make('name')
                    ->label('Name')
                    ->searchable(),
                TextColumn::make('slug')
                    ->label('Slug')
                    ->copyable()
                    ->toggleable(),
                TextColumn::make('images_path')
                    ->label('Upload-Pfad')
                    ->copyable()
                    ->toggleable(),
                TextColumn::make('sparkbooth_username')
                    ->label('Benutzername')
                    ->copyable()
                    ->toggleable(),
                TextColumn::make('created_at')
                    ->label('Angelegt')
                    ->since()
                    ->sortable(),
            ])
            ->actions([
                Action::make('show')
                    ->label('Zugangsdaten anzeigen')
                    ->icon('heroicon-o-key')
                    ->color('primary')
                    ->modalHeading('Upload-Zugangsdaten')
                    ->modalSubmitAction(false)
                    ->modalCancelActionLabel('Schliessen')
                    ->modalContent(function (Gallery $record) {
                        $plainToken = $record->regenerateUploadToken();

                        $data = [
                            'gallery' => $record->only(['id', 'name', 'slug', 'images_path']),
                            'upload_token' => $plainToken,
                            'upload_url' => route('api.sparkbooth.upload'),
                            'gallery_url' => route('gallery.show', $record),
                            'sparkbooth_username' => $record->sparkbooth_username,
                            'sparkbooth_password' => $record->sparkbooth_password,
                            'response_format' => $record->sparkbooth_response_format,
                        ];

                        Notification::make()
                            ->title('Zugangsdaten aktualisiert.')
                            ->body('Der Upload-Token wurde erneuert. Username/Passwort bleiben unveraendert.')
                            ->success()
                            ->send();

                        return view('filament.pages.partials.sparkbooth-token', $data);
                    }),
                Action::make('deleteConnection')
                    ->label('Verbindung loeschen')
                    ->icon('heroicon-o-trash')
                    ->color('danger')
                    ->requiresConfirmation()
                    ->modalHeading('Sparkbooth-Verbindung entfernen')
                    ->modalDescription('Die Galerie bleibt erhalten, aber Upload-Token und Zugangsdaten werden geloescht.')
                    ->action(function (Gallery $record): void {
                        $record->clearSparkboothConnection();

                        Notification::make()
                            ->title('Sparkbooth-Verbindung entfernt.')
                            ->body('Der Upload-Token und die Zugangsdaten wurden geloescht.')
                            ->success()
                            ->send();
                    }),
            ])
            ->emptyStateHeading('Keine Sparkbooth-Verbindungen')
            ->emptyStateDescription('Lege eine neue Verbindung an oder aktiviere Uploads fuer eine Galerie.');
    }
}
