<?php

namespace App\Filament\Resources\RetentionOverrideResource\Schemas;

use App\Enums\RetentionOverrideScope;
use App\Models\Event;
use App\Models\RetentionOverride;
use App\Models\Tenant;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Schemas\Schema;

class RetentionOverrideForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema->components([
            Section::make(__('admin.retention_overrides.sections.override'))
                ->schema([
                    Select::make('scope')
                        ->label(__('admin.retention_overrides.fields.scope'))
                        ->options([
                            RetentionOverrideScope::TENANT->value => __('admin.retention_overrides.scope.tenant'),
                            RetentionOverrideScope::EVENT->value => __('admin.retention_overrides.scope.event'),
                        ])
                        ->default(RetentionOverrideScope::TENANT->value)
                        ->required()
                        ->live()
                        ->disabled(fn (?RetentionOverride $record) => $record?->released_at !== null),
                    Select::make('tenant_id')
                        ->label(__('admin.retention_overrides.fields.tenant'))
                        ->options(Tenant::query()->orderBy('name')->pluck('name', 'id'))
                        ->searchable()
                        ->preload()
                        ->required()
                        ->live()
                        ->disabled(fn (?RetentionOverride $record) => $record?->released_at !== null),
                    Select::make('event_id')
                        ->label(__('admin.retention_overrides.fields.event'))
                        ->options(function (Get $get): array {
                            $tenantId = $get('tenant_id');
                            if (! $tenantId) {
                                return [];
                            }

                            return Event::query()
                                ->where('tenant_id', $tenantId)
                                ->orderByDesc('date')
                                ->get()
                                ->mapWithKeys(function (Event $event): array {
                                    $name = $event->name['de'] ?? $event->name['en'] ?? $event->slug;

                                    return [$event->id => $name];
                                })
                                ->all();
                        })
                        ->searchable()
                        ->preload()
                        ->visible(fn (Get $get): bool => $get('scope') === RetentionOverrideScope::EVENT->value)
                        ->required(fn (Get $get): bool => $get('scope') === RetentionOverrideScope::EVENT->value)
                        ->dehydrated(fn (Get $get): bool => $get('scope') === RetentionOverrideScope::EVENT->value)
                        ->disabled(fn (?RetentionOverride $record) => $record?->released_at !== null),
                    TextInput::make('reason')
                        ->label(__('admin.retention_overrides.fields.reason'))
                        ->maxLength(200)
                        ->required()
                        ->disabled(fn (?RetentionOverride $record) => $record?->released_at !== null),
                    Textarea::make('note')
                        ->label(__('admin.retention_overrides.fields.note'))
                        ->rows(3)
                        ->maxLength(2000)
                        ->columnSpanFull()
                        ->disabled(fn (?RetentionOverride $record) => $record?->released_at !== null),
                ])
                ->columns(2),
            Section::make(__('admin.retention_overrides.sections.status'))
                ->schema([
                    Placeholder::make('created_by_id')
                        ->label(__('admin.retention_overrides.fields.created_by'))
                        ->content(fn (?RetentionOverride $record) => $record?->createdBy?->name ?? '—'),
                    Placeholder::make('created_at')
                        ->label(__('admin.retention_overrides.fields.created_at'))
                        ->content(fn (?RetentionOverride $record) => $record?->created_at?->diffForHumans() ?? '—'),
                    Placeholder::make('released_by_id')
                        ->label(__('admin.retention_overrides.fields.released_by'))
                        ->content(fn (?RetentionOverride $record) => $record?->releasedBy?->name ?? '—'),
                    Placeholder::make('released_at')
                        ->label(__('admin.retention_overrides.fields.released_at'))
                        ->content(fn (?RetentionOverride $record) => $record?->released_at?->diffForHumans() ?? '—'),
                ])
                ->columns(2),
        ]);
    }
}
