<?php

namespace App\Filament\Resources\Publication\News\Schemas;

use App\Enums\NewsStatus;
use App\Models\News;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Builder;

class NewsForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Hidden::make('author_id')
                    ->default(auth()->id()),

                Section::make([
                    TextInput::make('title')
                        ->label('Judul')
                        ->placeholder('Berita Purwakarta Hari Ini')
                        ->autocomplete(false)
                        ->autofocus()
                        ->required()
                        ->maxLength(200),

                    RichEditor::make('content')
                        ->label('Konten')
                        ->placeholder('...')
                        ->required()
                        ->columnSpanFull(),

                    RichEditor::make('excerpt')
                        ->label('Ringkasan')
                        ->placeholder('...')
                        ->required()
                        ->columnSpanFull(),
                ])->columnSpan(2),

                Section::make([
                    TextInput::make('link')
                        ->label('Tautan')
                        ->placeholder('https://www.pwknews.com')
                        ->autocomplete(false)
                        ->nullable()
                        ->maxLength(255)
                        ->url(),

                    Select::make('categories')
                        ->label('Kategori')
                        ->relationship('categories', 'name', function (Builder $query): Builder {
                            return $query->active();
                        })
                        ->native(false)
                        ->multiple()
                        ->preload()
                        ->required(),

                    TagsInput::make('tags')
                        ->reactive()
                        ->afterStateHydrated(function ($component, $state, $record): void {
                            if (! $record) {
                                return;
                            }
                            $component->state(
                                $record->tags->pluck('name')->toArray()
                            );
                        })
                        ->helperText('Tekan enter untuk menambahkan tag.'),

                    Radio::make('status')
                        ->label('Status')
                        ->required()
                        ->options(NewsStatus::options())
                        ->default(NewsStatus::PUBLISHED->value)
                        ->inline()
                        ->live()
                        ->afterStateUpdated(function (Set $set, ?string $state, ?News $news): void {
                            if ($state == NewsStatus::PUBLISHED->value && ! $news?->published_at) {
                                $set('published_at', now());
                            } elseif ($state != NewsStatus::PUBLISHED->value && ! $news) {
                                $set('published_at', null);
                            }
                        })
                        ->rules(['in:'.implode(',', NewsStatus::values())]),

                    Hidden::make('published_at')
                        ->default(now()),

                    SpatieMediaLibraryFileUpload::make('thumbnail')
                        ->label('Thumbnail')
                        ->disk(config('filesystems.default'))
                        ->acceptedFileTypes(['image/*'])
                        ->maxSize(1024 * 3)
                        ->collection('news')
                        ->customProperties(fn (): array => [
                            'feature' => 'news',
                            'date' => now()->toDateString(),
                        ])
                        ->image()
                        ->required(),
                ]),
            ])
            ->columns(3);
    }
}
