<?php

namespace App\Filament\Pages\Media\Schemas;

use App\Enums\MediaClassification;
use App\Enums\MediaType;
use App\Enums\NotifStyle;
use App\Filament\Support\CheerfulNotification;
use Asmit\FilamentUpload\Forms\Components\AdvancedFileUpload;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Group;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class MediaForm
{
    public static function configure(Schema $schema): Schema
    {
        $documents = self::getDocuments();

        return $schema
            ->components([
                Section::make(fn () => CheerfulNotification::getByKey('media.section.title'))
                    ->description(fn () => CheerfulNotification::getByKey('media.section.description'))
                    ->icon(fn () => CheerfulNotification::getNotifStyle() === NotifStyle::CHEERFUL ? 'heroicon-o-newspaper' : null)
                    ->schema([
                        TextInput::make('name')
                            ->label('Nama')
                            ->placeholder('PWK News')
                            ->autocomplete(false)
                            ->autofocus()
                            ->required()
                            ->maxLength(50),

                        TextInput::make('link')
                            ->label('Tautan')
                            ->placeholder('https://www.pwknews.com')
                            ->autocomplete(false)
                            ->nullable()
                            ->maxLength(255)
                            ->url(),

                        Textarea::make('address')
                            ->label('Alamat')
                            ->placeholder('Jl. Kebontoro, Jakarta Pusat')
                            ->autocomplete(false)
                            ->required()
                            ->columnSpanFull(),

                        Radio::make('type')
                            ->label('Jenis')
                            ->options(MediaType::options())
                            ->inline()
                            ->required()
                            ->in(implode(',', array_column(MediaType::cases(), 'value'))),

                        Radio::make('classification')
                            ->label('Klasifikasi')
                            ->options(MediaClassification::options())
                            ->inline()
                            ->required()
                            ->in(implode(',', array_column(MediaClassification::cases(), 'value'))),
                    ])
                    ->columns(2)
                    ->columnSpan(2),

                Group::make()
                    ->schema(
                        collect($documents)
                            ->map(function (array $doc): Section {
                                return Section::make(fn () => $doc['title'])
                                    ->schema([
                                        TextInput::make($doc['text_name'])
                                            ->hiddenLabel()
                                            ->placeholder($doc['placeholder'])
                                            ->autocomplete(false)
                                            ->required()
                                            ->maxLength($doc['max']),

                                        AdvancedFileUpload::make($doc['file_name'])
                                            ->hiddenLabel()
                                            ->disk(config('filesystems.default'))
                                            ->acceptedFileTypes($doc['accept'])
                                            ->maxSize($doc['max_size'])
                                            ->directory($doc['folder'].now()->toDateString())
                                            ->helperText('Format yang diterima: '.implode(', ', array_map(fn ($t) => strtoupper(str_replace('application/', '', $t)), $doc['accept'])).'. Ukuran maksimum: '.($doc['max_size'] / 1024).' MB.'),
                                    ]);
                            })->toArray()
                    )
                    ->columns(2),
            ])
            ->statePath('data');
    }

    public static function getDocuments(): array
    {
        return [
            [
                'title' => 'Organisasi Kewartawanan',
                'text_name' => 'journalism_organization',
                'placeholder' => '*****',
                'max' => 255,
                'max_size' => 1024 * 10,
                'file_name' => 'journalism_organization_docs',
                'accept' => ['application/pdf'],
                'folder' => 'media/journalism-organization/',
            ],
            [
                'title' => 'Sertifikat Dewan Pers',
                'text_name' => 'press_council_certificate',
                'placeholder' => '*****',
                'max' => 255,
                'max_size' => 1024 * 10,
                'file_name' => 'press_council_certificate_docs',
                'accept' => ['application/pdf'],
                'folder' => 'media/press-council-certificate/',
            ],
        ];
    }
}
