<?php

namespace App\Filament\Pages\Company\Schemas;

use App\Enums\NotifStyle;
use App\Filament\Support\CheerfulNotification;
use Asmit\FilamentUpload\Forms\Components\AdvancedFileUpload;
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 CompanyForm
{
    public static function configure(Schema $schema): Schema
    {
        $documents = self::getDocuments();

        return $schema
            ->components([
                Section::make(fn () => CheerfulNotification::getByKey('company.section.title'))
                    ->description(fn () => CheerfulNotification::getByKey('company.section.description'))
                    ->icon(fn () => CheerfulNotification::getNotifStyle() === NotifStyle::CHEERFUL ? 'heroicon-o-building-office-2' : null)
                    ->schema([
                        TextInput::make('name')
                            ->label('Nama')
                            ->placeholder('PT ABC Indonesia Abadi')
                            ->autocomplete(false)
                            ->autofocus()
                            ->required()
                            ->maxLength(100),

                        TextInput::make('director_name')
                            ->label('Nama Direktur')
                            ->placeholder('John Doe')
                            ->autocomplete(false)
                            ->required()
                            ->maxLength(150),

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

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

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

                Group::make()
                    ->schema(
                        array_merge(
                            [
                                Section::make('NIk Direktur')
                                    ->schema([
                                        TextInput::make('director_nik')
                                            ->hiddenLabel()
                                            ->placeholder('32***')
                                            ->autocomplete(false)
                                            ->required()
                                            ->maxLength(16)
                                            ->rule('digits:16')
                                            ->inputMode('numeric')
                                            ->regex('/^[0-9]+$/'),

                                        AdvancedFileUpload::make('director_nik_docs')
                                            ->hiddenLabel()
                                            ->disk(config('filesystems.default'))
                                            ->acceptedFileTypes(['image/*'])
                                            ->maxSize(1024 * 3)
                                            ->directory('companies/director-nik/'.now()->toDateString())
                                            ->helperText('Format yang diterima: JPEG, PNG. Ukuran maksimum: 3 MB.')
                                            ->required(),
                                    ]),
                            ],

                            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.')
                                                ->required(),
                                        ]);
                                })->toArray()
                        )
                    )
                    ->columns(2),
            ])
            ->statePath('data');
    }

    public static function getDocuments(): array
    {
        return [
            [
                'title' => 'Akta Pendirian',
                'text_name' => 'deed_incorporation',
                'placeholder' => '*****',
                'max' => 150,
                'max_size' => 1024 * 10,
                'file_name' => 'deed_incorporation_docs',
                'accept' => ['application/pdf'],
                'folder' => 'companies/deed-incorporation/',
            ],
            [
                'title' => 'SIUP / NIB',
                'text_name' => 'trade_license',
                'placeholder' => '*****',
                'max' => 150,
                'max_size' => 1024 * 10,
                'file_name' => 'trade_license_docs',
                'accept' => ['application/pdf'],
                'folder' => 'companies/trade-license/',
            ],
            [
                'title' => 'NPWP',
                'text_name' => 'tax_id_number',
                'placeholder' => '*****',
                'max' => 150,
                'max_size' => 1024 * 10,
                'file_name' => 'tax_id_number_docs',
                'accept' => ['application/pdf'],
                'folder' => 'companies/tax-id-number/',
            ],
            [
                'title' => 'PKP',
                'text_name' => 'taxable_enterprise',
                'placeholder' => '*****',
                'max' => 150,
                'max_size' => 1024 * 10,
                'file_name' => 'taxable_enterprise_docs',
                'accept' => ['application/pdf'],
                'folder' => 'companies/taxable-enterprise/',
            ],
            [
                'title' => 'SPT Tahunan',
                'text_name' => 'annual_tax_return',
                'placeholder' => '*****',
                'max' => 150,
                'max_size' => 1024 * 10,
                'file_name' => 'annual_tax_return_docs',
                'accept' => ['application/pdf'],
                'folder' => 'companies/annual-tax-return/',
            ],
            [
                'title' => 'Suket Domisili',
                'text_name' => 'domicile_certificate',
                'placeholder' => '*****',
                'max' => 150,
                'max_size' => 1024 * 10,
                'file_name' => 'domicile_certificate_docs',
                'accept' => ['application/pdf'],
                'folder' => 'companies/domicile-certificate/',
            ],
            [
                'title' => 'Profil Perusahaan',
                'text_name' => 'profile',
                'placeholder' => '*****',
                'max' => 150,
                'max_size' => 1024 * 10,
                'file_name' => 'profile_docs',
                'accept' => ['application/pdf'],
                'folder' => 'companies/profile/',
            ],
        ];
    }
}
