<?php

declare(strict_types=1);

namespace App\Filament\Resources\Attachments\Schemas;

use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;

class AttachmentForm
{
    public static function configure(Schema $schema): Schema
    {
        $disk = config('larablog.attachments_disk', 'attachments');
        $mimes = config('larablog.allowed_attachment_mimes', []);

        return $schema
            ->components([
                Select::make('article_id')
                    ->label(__('admin.fields.article'))
                    ->relationship('article', 'title')
                    ->searchable()
                    ->preload(),
                FileUpload::make('upload')
                    ->label(__('admin.fields.upload'))
                    ->disk($disk)
                    ->directory(fn (): string => 'uploads/'.now()->format('Y/m'))
                    ->visibility('public')
                    ->acceptedFileTypes($mimes)
                    ->maxSize(20480)
                    ->required(fn (string $operation): bool => $operation === 'create')
                    ->dehydrated(fn ($state): bool => filled($state))
                    ->helperText(__('admin.helpers.attachment_upload')),
                TextInput::make('filename')
                    ->label(__('admin.fields.filename'))
                    ->maxLength(255),
                TextInput::make('visibility')
                    ->label(__('admin.fields.visibility'))
                    ->default('public')
                    ->required(),
                TextInput::make('legacy_filepath')
                    ->label(__('admin.fields.legacy_filepath'))
                    ->maxLength(255),
                TextInput::make('downloads')
                    ->label(__('admin.fields.downloads'))
                    ->numeric()
                    ->default(0)
                    ->disabled()
                    ->dehydrated(),
            ]);
    }
}
