<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpOtherStockOutOrders\Pages;

use App\Business\Inventory\Enums\InventoryDocumentStatus;
use App\Business\Inventory\OtherStockOutService;
use App\Filament\Resources\DberpOtherStockOutOrders\DberpOtherStockOutOrderResource;
use App\Filament\Resources\Pages\EditRecord;
use App\Filament\Support\DberpPrintFilamentAction;
use App\Models\DberpOtherStockOutOrder;
use App\Support\DberpFilamentInventoryConfirmActionFeedback;
use App\Support\DberpFilamentInventoryVoidActionConfigurator;
use App\Support\Print\Documents\OtherStockOutPrintPresenter;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;

class EditDberpOtherStockOutOrder extends EditRecord
{
    protected static string $resource = DberpOtherStockOutOrderResource::class;

    protected function getHeaderActions(): array
    {
        return [
            DberpPrintFilamentAction::forDocument(
                OtherStockOutPrintPresenter::DOCUMENT_KEY,
                fn (DberpOtherStockOutOrder $record): bool => DberpOtherStockOutOrderResource::canView($record),
            ),
            Action::make('apply')
                ->label(__('dberp.actions.confirm_take_effect'))
                ->requiresConfirmation()
                ->modalDescription(__('dberp.hints.inventory_apply_outbound'))
                ->visible(fn (DberpOtherStockOutOrder $record): bool => $record->status === InventoryDocumentStatus::Draft->value)
                ->action(function (): void {
                    DberpFilamentInventoryConfirmActionFeedback::runWithFailureNotification(function (): void {
                        app(OtherStockOutService::class)->apply($this->record);
                    });
                    $this->record->refresh();
                    $this->fillForm();
                }),
            DberpFilamentInventoryVoidActionConfigurator::configureEffectiveInventoryVoidAction(
                Action::make('void')
                    ->requiresConfirmation()
                    ->visible(fn (DberpOtherStockOutOrder $record): bool => $record->status === InventoryDocumentStatus::Effective->value)
                    ->action(function (): void {
                        DberpFilamentInventoryConfirmActionFeedback::runWithFailureNotification(function (): void {
                            app(OtherStockOutService::class)->void($this->record);
                        });
                        $this->record->refresh();
                        $this->fillForm();
                        DberpFilamentInventoryVoidActionConfigurator::notifyInventoryRevokeSuccess();
                    }),
            ),
            DeleteAction::make()
                ->visible(fn (DberpOtherStockOutOrder $record): bool => $record->status === InventoryDocumentStatus::Draft->value),
        ];
    }
}
