<?php

namespace App\Filament\Resources\Monitoring\MediaMonitorings\Schemas;

use App\Enums\Channel;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Builder;

class MediaMonitoringForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make([
                    TextInput::make('media_name')
                        ->label('Nama Media')
                        ->placeholder('PWK News')
                        ->autocomplete(false)
                        ->autofocus()
                        ->required()
                        ->maxLength(50),

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

                    Grid::make(2)
                        ->schema([
                            TextInput::make('writter')
                                ->label('Penulis')
                                ->placeholder('John Doe')
                                ->autocomplete(false)
                                ->required()
                                ->maxLength(50),

                            TextInput::make('news_page')
                                ->label('Halaman Berita')
                                ->placeholder('Halaman 1 s/d 10')
                                ->autocomplete(false)
                                ->nullable()
                                ->maxLength(20),
                        ]),

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

                    RichEditor::make('quote')
                        ->label('Kutipan')
                        ->placeholder('....')
                        ->nullable(),

                    RichEditor::make('content')
                        ->label('Konten')
                        ->placeholder('....')
                        ->required(),
                ])->columnSpan(2),

                Section::make([
                    Select::make('channel')
                        ->label('Kanal')
                        ->options(Channel::options())
                        ->native(false)
                        ->required()
                        ->in(implode(',', array_column(Channel::cases(), 'value'))),

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

                    DatePicker::make('release_date')
                        ->label('Tanggal Rilis')
                        ->placeholder(fn (): string => now()->translatedFormat('l, d F Y'))
                        ->native(false)
                        ->displayFormat('l, d F Y')
                        ->required(),

                    TextInput::make('influencer')
                        ->label('Influencer')
                        ->placeholder('Sekretariat Daerah')
                        ->autocomplete(false)
                        ->nullable()
                        ->maxLength(50),

                    TextInput::make('keyword')
                        ->label('Kata Kunci')
                        ->placeholder('Purwakarta, Berita Terbaru, Hari Ini')
                        ->autocomplete(false)
                        ->required()
                        ->maxLength(100),

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