<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpOtherStockInOrders\Pages;

use App\Business\Inventory\Enums\InventoryDocumentStatus;
use App\Business\Inventory\OtherStockInService;
use App\Filament\Resources\DberpOtherStockInOrders\DberpOtherStockInOrderResource;
use App\Filament\Resources\Pages\EditRecord;
use App\Filament\Support\DberpPrintFilamentAction;
use App\Models\DberpOtherStockInOrder;
use App\Support\DberpFilamentInventoryConfirmActionFeedback;
use App\Support\DberpFilamentInventoryVoidActionConfigurator;
use App\Support\Print\Documents\OtherStockInPrintPresenter;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;

class EditDberpOtherStockInOrder extends EditRecord
{
    protected static string $resource = DberpOtherStockInOrderResource::class;

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