<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpSalesOutbounds\Pages;

use App\Business\Inventory\Enums\InventoryDocumentStatus;
use App\Business\MasterData\DberpDocumentAddressSnapshotService;
use App\Business\Sales\SalesOutboundService;
use App\Filament\Resources\DberpSalesOutbounds\DberpSalesOutboundResource;
use App\Filament\Resources\Pages\EditRecord;
use App\Filament\Support\DberpPrintFilamentAction;
use App\Models\DberpSalesOutboundOrder;
use App\Support\DberpFilamentInventoryConfirmActionFeedback;
use App\Support\DberpFilamentInventoryVoidActionConfigurator;
use App\Support\Print\Documents\SalesOutboundPrintPresenter;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;

class EditDberpSalesOutbound extends EditRecord
{
    protected static string $resource = DberpSalesOutboundResource::class;

    protected function mutateFormDataBeforeSave(array $data): array
    {
        $snapshotService = app(DberpDocumentAddressSnapshotService::class);
        $record = $this->getRecord();
        $data = $snapshotService->fillCustomerSnapshotWhenNeededForSalesOutbound(
            $data,
            $record instanceof DberpSalesOutboundOrder ? $record : null,
        );

        return $snapshotService->fillWarehouseSnapshot($data);
    }

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