<?php

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

use App\Models\PartnerMedia;
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\Schema;
use Filament\Support\Icons\Heroicon;

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 () => now()->format('Y-m-d'))
                                ->native(false)
                                ->displayFormat('l, d F Y')
                                ->required(),

                            DatePicker::make('final_submission_date')
                                ->label('Tanggal Pengajuan Akhir')
                                ->placeholder(fn () => now()->addDays(30)->format('Y-m-d'))
                                ->native(false)
                                ->displayFormat('l, d F Y')
                                ->required()
                                ->after('initial_submission_date'),
                        ]),

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

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

                Section::make([
                    SpatieMediaLibraryFileUpload::make('banner')
                        ->label('Banner')
                        ->disk(config('filesystems.default'))
                        ->acceptedFileTypes(['image/*'])
                        ->maxSize(1024 * 3)
                        ->collection('cooperation')
                        ->image(),

                    SpatieMediaLibraryFileUpload::make('proposal_template')
                        ->label('Template Pengajuan')
                        ->disk(config('filesystems.default'))
                        ->acceptedFileTypes(['application/pdf'])
                        ->maxSize(1024 * 10)
                        ->collection('cooperation')
                        ->required(),
                ]),
            ])
            ->columns(3);
    }
}
