<?php

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

use App\Enums\NewsStatus;
use App\Enums\NotifStyle;
use App\Filament\Support\CheerfulNotification;
use App\Models\News;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\RichEditor;
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([
                Hidden::make('author_id')
                    ->default(auth()->id()),

                Section::make(CheerfulNotification::getByKey('news.content.title'))
                    ->description(CheerfulNotification::getByKey('news.content.desc'))
                    ->icon(CheerfulNotification::getNotifStyle() === NotifStyle::CHEERFUL ? 'heroicon-o-pencil-square' : null)
                    ->schema([
                        TextInput::make('title')
                            ->label('Judul Berita')
                            ->placeholder('Berita Purwakarta Hari Ini')
                            ->autocomplete(false)
                            ->autofocus()
                            ->required()
                            ->maxLength(200),

                        RichEditor::make('excerpt')
                            ->label('Ringkasan (Excerpt)')
                            ->placeholder('Berikan ringkasan singkat berita...')
                            ->required()
                            ->columnSpanFull(),

                        RichEditor::make('content')
                            ->label('Isi Lengkap Berita')
                            ->placeholder('Tuliskan konten berita secara mendetail...')
                            ->required()
                            ->columnSpanFull(),
                    ])->columnSpan(2),

                Section::make(CheerfulNotification::getByKey('news.content.publication_title'))
                    ->description(CheerfulNotification::getByKey('news.content.publication_desc'))
                    ->icon(CheerfulNotification::getNotifStyle() === NotifStyle::CHEERFUL ? 'heroicon-o-tag' : null)
                    ->schema([
                        TextInput::make('link')
                            ->label('Tautan Media (Opsional)')
                            ->placeholder('https://www.pwknews.com')
                            ->autocomplete(false)
                            ->nullable()
                            ->maxLength(255)
                            ->url(),

                        TagsInput::make('tags')
                            ->label('Tag Berita')
                            ->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 Publikasi')
                            ->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())]),

                        SpatieMediaLibraryFileUpload::make('thumbnail')
                            ->label('Thumbnail Utama')
                            ->disk(config('filesystems.default'))
                            ->acceptedFileTypes(['image/*'])
                            ->maxSize(1024 * 3)
                            ->collection('news')
                            ->customProperties(fn (): array => [
                                'feature' => 'news',
                                'date' => now()->toDateString(),
                            ])
                            ->image()
                            ->helperText('Format yang diterima: JPEG, PNG, GIF, WEBP. Ukuran maksimum: 3 MB.')
                            ->required(),
                    ])->columnSpan(1),
            ])
            ->columns(3);
    }
}
