<?php

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

use App\Enums\NotifStyle;
use App\Filament\Support\CheerfulNotification;
use App\Models\PartnerMedia;
use Asmit\FilamentUpload\Forms\Components\AdvancedFileUpload;
use Filament\Actions\Action;
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\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Builder;

class CooperationForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(fn () => CheerfulNotification::getByKey('cooperation.detail_title'))
                    ->description(fn () => CheerfulNotification::getByKey('cooperation.detail_desc'))
                    ->icon(fn () => CheerfulNotification::getNotifStyle() === NotifStyle::CHEERFUL ? 'heroicon-o-hand-raised' : null)
                    ->schema([
                        TextInput::make('title')
                            ->label('Judul Kerja Sama')
                            ->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(fn ($context) => $context === 'create' ? today() : null)
                                    ->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 Rekanan')
                            ->relationship('partnerMedia', 'name', fn (Builder $query): Builder => $query->verified())
                            ->multiple()
                            ->preload()
                            ->searchable()
                            ->native(false)
                            ->required()
                            ->selectablePlaceholder(false)
                            ->helperText('Pilih satu atau lebih media yang akan diajak kerja sama.')
                            ->suffixAction(
                                Action::make('select_all')
                                    ->label('Pilih Semua')
                                    ->icon('heroicon-o-check-circle')
                                    ->action(function (Set $set): void {
                                        $allMediaIds = PartnerMedia::query()->verified()->pluck('id')->toArray();
                                        $set('partner_media_ids', $allMediaIds);
                                    })
                            ),

                        RichEditor::make('description')
                            ->label('Deskripsi Kerja Sama')
                            ->placeholder('Jelaskan detail lingkup kerja sama di sini...')
                            ->required(),
                    ])
                    ->columnSpan(2),

                Section::make(fn () => CheerfulNotification::getByKey('cooperation.attachment_title'))
                    ->description(fn () => CheerfulNotification::getByKey('cooperation.attachment_desc'))
                    ->icon(fn () => CheerfulNotification::getNotifStyle() === NotifStyle::CHEERFUL ? 'heroicon-o-paper-clip' : null)
                    ->schema([
                        SpatieMediaLibraryFileUpload::make('banner')
                            ->label('Banner Promosi')
                            ->disk(config('filesystems.default'))
                            ->acceptedFileTypes(['image/*'])
                            ->maxSize(1024 * 3)
                            ->collection('banner')
                            ->customProperties(fn (): array => [
                                'feature' => 'cooperations',
                                'doc_type' => 'banner',
                                'date' => now()->toDateString(),
                            ])
                            ->image()
                            ->helperText('Format yang diterima: JPEG, PNG, GIF, WEBP. Ukuran maksimum: 3 MB.'),

                        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())
                            ->helperText('Format yang diterima: PDF. Ukuran maksimum: 10 MB.'),
                    ])->columnSpan(1),
            ])
            ->columns(3);
    }
}
