<?php

namespace App\Filament\Pages\Media\Actions;

use App\Filament\Support\CheerfulNotification;
use Filament\Actions\Action;
use Filament\Forms\Components\Textarea;
use Filament\Support\Enums\Width;

class SaveMediaAction extends Action
{
    public static function getDefaultName(): ?string
    {
        return 'save';
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->label('Simpan')
            ->modalHeading(fn () => CheerfulNotification::getByKey('media.save.title'))
            ->modalDescription(fn () => CheerfulNotification::getByKey('media.save.description'))
            ->modal(fn (): bool => (bool) ($this->getLivewire()?->needsMediaReview()))
            ->schema(fn (): array => ($this->getLivewire()?->needsMediaReview())
                ? [
                    Textarea::make('change_reason')
                        ->label('Alasan')
                        ->placeholder('Jelaskan alasan perubahan data ini...')
                        ->autocomplete(false)
                        ->autofocus()
                        ->required(),
                ]
                : [])
            ->action(function (array $data = []): void {
                $this->getLivewire()?->handleMediaSave($data);
            })
            ->modalWidth(Width::Large);
    }
}
