<?php

namespace App\Filament\Pages\Verification\Actions;

use App\Enums\VerificationStatus;
use App\Filament\Support\CheerfulNotification;
use Filament\Actions\Action;

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

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

        $this->label(function (): string {
            $progress = $this->getLivewire()->getVerificationProgress();
            $verification = $progress['verification'];

            return $verification && $verification->status === VerificationStatus::NEED_REVISION
                ? 'Kirim Ulang'
                : 'Ajukan Verifikasi';
        })
            ->icon('heroicon-o-paper-airplane')
            ->color('primary')
            ->requiresConfirmation()
            ->modalHeading(fn () => CheerfulNotification::getByKey('verification.submit.title'))
            ->modalDescription(function (): string {
                $progress = $this->getLivewire()->getVerificationProgress();

                if (! $progress['isDataComplete']) {
                    return CheerfulNotification::getByKey(
                        'verification.submit.description.incomplete',
                        ['missing' => implode(', ', $progress['missingData'])]
                    );
                }

                $verification = $progress['verification'];

                if ($verification && $verification->status === VerificationStatus::NEED_REVISION) {
                    return CheerfulNotification::getByKey('verification.submit.description.revision');
                }

                return CheerfulNotification::getByKey('verification.submit.description.default');
            })
            ->modalSubmitActionLabel('Ya, Ajukan')
            ->visible(function (): bool {
                return $this->getLivewire()->getVerificationProgress()['canSubmit'];
            })
            ->disabled(function (): bool {
                return ! $this->getLivewire()->getVerificationProgress()['isDataComplete'];
            })
            ->action(function (array $data = []): void {
                $this->getLivewire()->handleVerificationSubmit($data);
            });
    }
}
