<?php

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

use App\Enums\RoleEnum;
use App\Enums\VerificationStatus;
use Asmit\FilamentUpload\Forms\Components\AdvancedFileUpload;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Group;
use Filament\Schemas\Schema;

class JournalistForm
{
    public static function configure(Schema $schema): Schema
    {
        $documents = [
            [
                'title' => 'Dokumen Pers',
                'text_name' => 'press_card',
                'placeholder' => '*****',
                'max' => 100,
                'max_size' => 1024 * 10,
                'file_name' => 'press_card_docs',
                'accept' => ['application/pdf'],
                'folder' => 'press-card/',
            ],
            [
                'title' => 'Sertifikat UKW',
                'text_name' => 'ukw_certificate',
                'placeholder' => '*****',
                'max' => 100,
                'max_size' => 1024 * 10,
                'file_name' => 'ukw_certificate_docs',
                'accept' => ['application/pdf'],
                'folder' => 'ukw-certificate/',
            ],
        ];

        return $schema
            ->components([
                TextInput::make('name')
                    ->label('Nama Lengkap')
                    ->placeholder('John Doe')
                    ->autocomplete(false)
                    ->autofocus()
                    ->required()
                    ->maxLength(100),

                TextInput::make('email')
                    ->label('Alamat Surel')
                    ->placeholder('johndoe@example.com')
                    ->autocomplete(false)
                    ->required()
                    ->maxLength(254)
                    ->email()
                    ->prefixIcon('heroicon-o-envelope'),

                TextInput::make('phone_number')
                    ->label('Nomor Telepon')
                    ->placeholder('08xx xxxx xxxx')
                    ->autocomplete(false)
                    ->required()
                    ->maxLength(20)
                    ->tel()
                    ->prefixIcon('heroicon-o-phone'),

                Group::make()
                    ->schema(
                        collect($documents)
                            ->map(function (array $doc): array {
                                return [
                                    TextInput::make($doc['text_name'])
                                        ->label('Nomor Identitas ('.$doc['title'].')')
                                        ->placeholder($doc['placeholder'])
                                        ->autocomplete(false)
                                        ->required()
                                        ->maxLength($doc['max']),

                                    AdvancedFileUpload::make($doc['file_name'])
                                        ->label('File Pendukung ('.$doc['title'].') (PDF)')
                                        ->disk(config('filesystems.default'))
                                        ->acceptedFileTypes($doc['accept'])
                                        ->directory(fn (): string => 'journalists/'.$doc['folder'].now()->toDateString())
                                        ->maxSize($doc['max_size'])
                                        ->helperText('Format yang diterima: '.implode(', ', array_map(fn ($t) => strtoupper(str_replace('application/', '', $t)), $doc['accept'])).'. Ukuran maksimum: '.($doc['max_size'] / 1024).' MB.')
                                        ->required(),
                                ];
                            })
                            ->flatten()
                            ->toArray()
                    ),

                Textarea::make('change_reason')
                    ->label('Alasan Perubahan')
                    ->placeholder('Jelaskan alasan perubahan data ini...')
                    ->rows(3)
                    ->required()
                    ->visible(fn () => auth()->user()->company?->verificationRequest?->status === VerificationStatus::APPROVED && auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value)),
            ])
            ->columns(1);
    }
}
