<?php

namespace App\Filament\Resources\Galleries\Schemas;

use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Schemas\Components\View;
use Filament\Schemas\Schema;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;

class GalleryForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->columns(1)
            ->components([
                Tabs::make('gallery_tabs')
                    ->tabs([
                        Tab::make('Galerie')
                            ->schema([
                                Section::make('Details')
                                    ->columns(2)
                                    ->schema([
                                        TextInput::make('name')
                                            ->label('Name')
                                            ->required(),
                                        TextInput::make('slug')
                                            ->label('Slug')
                                            ->disabled()
                                            ->dehydrated(true)
                                            ->helperText('Wird automatisch erzeugt'),
                                        TextInput::make('title')
                                            ->label('Titel')
                                            ->required(),
                                        TextInput::make('images_path')
                                            ->label('Bilder-Pfad')
                                            ->helperText('Relativer Pfad unter public/storage')
                                            ->required(),
                                        Toggle::make('is_public')
                                            ->label('Öffentlich')
                                            ->default(true),
                                        Toggle::make('allow_ai_styles')
                                            ->label('AI-Stile erlauben')
                                            ->default(true),
                                        Toggle::make('allow_print')
                                            ->label('Drucken erlauben')
                                            ->default(true),
                                        Toggle::make('require_password')
                                            ->label('Passwortschutz aktiv')
                                            ->default(false),
                                        TextInput::make('password')
                                            ->label('Neues Passwort')
                                            ->password()
                                            ->revealable()
                                            ->dehydrated(false)
                                            ->helperText('Leer lassen, um das bestehende Passwort zu behalten.'),
                                        DateTimePicker::make('expires_at')
                                            ->label('Ablaufdatum')
                                            ->native(false)
                                            ->seconds(false),
                                        TextInput::make('access_duration_minutes')
                                            ->label('Zugriffsdauer (Minuten)')
                                            ->numeric()
                                            ->minValue(1)
                                            ->nullable()
                                            ->helperText('Optional: Zeitfenster nach dem ersten Unlock.'),
                                    ]),
                                View::make('filament.components.gallery-link')
                                    ->columnSpanFull()
                                    ->visible(fn (?object $record) => (bool) $record?->id)
                                    ->viewData(fn (?object $record) => [
                                        'url' => $record ? URL::route('gallery.show', $record) : null,
                                    ]),
                            ]),
                        Tab::make('Sparkbooth')
                            ->schema([
                                Section::make('Sparkbooth Upload')
                                    ->columns(2)
                                    ->schema([
                                        Toggle::make('upload_enabled')
                                            ->label('Uploads aktivieren')
                                            ->helperText('Steuert, ob Sparkbooth-Uploads erlaubt sind.')
                                            ->default(false),
                                        Select::make('sparkbooth_response_format')
                                            ->label('Standard-Antwortformat')
                                            ->options([
                                                'json' => 'JSON',
                                                'xml' => 'XML',
                                            ])
                                            ->default('json'),
                                        TextInput::make('sparkbooth_username')
                                            ->label('Sparkbooth Benutzername')
                                            ->helperText('Wird in Sparkbooth unter „Username“ eingetragen. Erlaubt sind Buchstaben, Zahlen sowie ._-')
                                            ->maxLength(64)
                                            ->rule('regex:/^[A-Za-z0-9._-]+$/')
                                            ->unique(table: \App\Models\Gallery::class, column: 'sparkbooth_username', ignoreRecord: true)
                                            ->dehydrateStateUsing(fn (?string $state): ?string => $state ? Str::of($state)->lower()->trim()->value() : null),
                                        TextInput::make('sparkbooth_password')
                                            ->label('Sparkbooth Passwort (neu)')
                                            ->password()
                                            ->revealable()
                                            ->dehydrated(false)
                                            ->afterStateHydrated(fn (callable $set) => $set('sparkbooth_password', null))
                                            ->helperText('Leer lassen, um das bestehende Passwort zu behalten.'),
                                    ]),
                                View::make('filament.pages.partials.sparkbooth-token')
                                    ->columnSpanFull()
                                    ->visible(fn (?object $record) => (bool) $record?->id)
                                    ->viewData(fn (?object $record) => [
                                        'upload_url' => URL::route('api.sparkbooth.upload'),
                                        'gallery_url' => $record ? URL::route('gallery.show', $record) : null,
                                        'sparkbooth_username' => $record?->sparkbooth_username,
                                        'sparkbooth_password' => $record?->sparkbooth_password,
                                        'response_format' => $record?->sparkbooth_response_format,
                                        'gallery' => $record ? $record->only(['slug', 'images_path']) : null,
                                    ]),
                            ]),
                    ]),
            ]);
    }
}
