<?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;

class NewsForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                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(),

                    TextInput::make('link')
                        ->label('Tautan')
                        ->placeholder('https://www.pwknews.com')
                        ->autocomplete(false)
                        ->nullable()
                        ->maxLength(50)
                        ->url(),
                ])->columnSpan(2),

                Section::make([
                    Select::make('categories')
                        ->label('Kategori')
                        ->relationship('categories', 'name', fn ($query) => $query->active())
                        ->native(false)
                        ->multiple()
                        ->preload()
                        ->required(),

                    TagsInput::make('tags')
                        ->reactive()
                        ->afterStateHydrated(function ($component, $state, $record) {
                            if (! $record) {
                                return;
                            }
                            $component->state(
                                $record->tags->pluck('name')->toArray()
                            );
                        }),

                    Radio::make('status')
                        ->label('Status')
                        ->required()
                        ->options(NewsStatus::options())
                        ->default(NewsStatus::PUBLISHED->value)
                        ->inline()
                        ->live()
                        ->afterStateUpdated(function (Set $set, ?string $state, ?News $news) {
                            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')
                        ->image()
                        ->required(),
                ]),
            ])
            ->columns(3);
    }
}
