<?php

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

use App\Enums\AnnouncementType;
use Filament\Actions\Action;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Builder;

class AnnouncementForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('title')
                    ->label('Judul Pengumuman')
                    ->placeholder('Pencairan Dana Kerja Sama Media')
                    ->autocomplete(false)
                    ->autofocus()
                    ->required()
                    ->columnSpanFull(),

                RichEditor::make('content')
                    ->label('Isi Pengumuman')
                    ->placeholder('Tuliskan detail pengumuman di sini...')
                    ->required()
                    ->columnSpanFull(),

                Radio::make('type')
                    ->label('Tipe Pengumuman')
                    ->options(AnnouncementType::class)
                    ->required()
                    ->default(AnnouncementType::PUBLIC->value)
                    ->live()
                    ->inline(),

                CheckboxList::make('partnerMedia')
                    ->label('Penerima Khusus')
                    ->relationship('partnerMedia', 'name', fn (Builder $query): Builder => $query->verified())
                    ->visible(fn (Get $get): bool => in_array($get('type'), [AnnouncementType::COMPANY, AnnouncementType::COMPANY->value]))
                    ->required(fn (Get $get): bool => in_array($get('type'), [AnnouncementType::COMPANY, AnnouncementType::COMPANY->value]))
                    ->searchable()
                    ->noSearchResultsMessage('Tidak ada media yang ditemukan.')
                    ->helperText('Pilih media yang akan menerima pengumuman ini.')
                    ->columnSpanFull()
                    ->bulkToggleable()
                    ->selectAllAction(
                        fn (Action $action): Action => $action->label('Pilih Semua Media'),
                    )
                    ->dehydrated(),
            ])
            ->columns(1);
    }
}
