<?php

namespace App\Filament\Resources\Manage\Cooperations\Schemas;

use App\Models\PartnerMedia;
use Asmit\FilamentUpload\Forms\Components\AdvancedFileUpload;
use Filament\Actions\Action;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Illuminate\Database\Eloquent\Builder;

class CooperationForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make([
                    TextInput::make('title')
                        ->label('Judul')
                        ->placeholder('Publikasi Pembangunan Daerah')
                        ->autocomplete(false)
                        ->autofocus()
                        ->required()
                        ->maxLength(200),

                    Grid::make(2)
                        ->schema([
                            DatePicker::make('initial_submission_date')
                                ->label('Tanggal Pengajuan Awal')
                                ->placeholder(fn (): string => now()->translatedFormat('l, d F Y'))
                                ->native(false)
                                ->displayFormat('l, d F Y')
                                ->required()
                                ->minDate(today())
                                ->reactive(),

                            DatePicker::make('final_submission_date')
                                ->label('Tanggal Pengajuan Akhir')
                                ->placeholder(fn (): string => now()->addDays(7)->translatedFormat('l, d F Y'))
                                ->native(false)
                                ->displayFormat('l, d F Y')
                                ->required()
                                ->afterOrEqual('initial_submission_date')
                                ->minDate(fn (Get $get) => $get('initial_submission_date')),
                        ]),

                    Select::make('partner_media_ids')
                        ->label('Media')
                        ->relationship('partnerMedia', 'name', fn (Builder $query): Builder => $query->verified())
                        ->multiple()
                        ->preload()
                        ->searchable()
                        ->native(false)
                        ->required()
                        ->selectablePlaceholder(false)
                        ->helperText('Pilih media yang akan diajak kerja sama.')
                        ->suffixAction(
                            Action::make('select_all')
                                ->label('Select All')
                                ->icon(Heroicon::OutlinedCheckCircle)
                                ->action(function (Set $set): void {
                                    $allMediaIds = PartnerMedia::verified()->pluck('id')->toArray();
                                    $set('partner_media_ids', $allMediaIds);
                                })
                        ),

                    Textarea::make('description')
                        ->label('Deskripsi')
                        ->placeholder('...')
                        ->required()
                        ->rows(5)
                        ->maxLength(65535)
                        ->autocomplete(false),
                ])
                    ->columnSpan(2),

                Section::make([
                    SpatieMediaLibraryFileUpload::make('banner')
                        ->label('Banner')
                        ->disk(config('filesystems.default'))
                        ->acceptedFileTypes(['image/*'])
                        ->maxSize(1024 * 3)
                        ->collection('banner')
                        ->customProperties(fn (): array => [
                            'feature' => 'cooperations',
                            'doc_type' => 'banner',
                            'date' => now()->toDateString(),
                        ])
                        ->image(),

                    AdvancedFileUpload::make('proposal_template')
                        ->label('Template Pengajuan')
                        ->disk(config('filesystems.default'))
                        ->acceptedFileTypes(['application/pdf'])
                        ->maxSize(1024 * 10)
                        ->directory(fn (): string => 'cooperations/proposal-template/'.now()->toDateString())
                        ->required(),
                ]),
            ])
            ->columns(3);
    }
}
