<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpStocktakeOrders\Pages;

use App\Business\Inventory\Enums\InventoryDocumentStatus;
use App\Business\Inventory\StocktakeOrderService;
use App\Filament\Resources\DberpStocktakeOrders\DberpStocktakeOrderResource;
use App\Filament\Resources\Pages\EditRecord;
use App\Filament\Support\DberpPrintFilamentAction;
use App\Models\DberpStocktakeOrder;
use App\Support\DberpFilamentInventoryConfirmActionFeedback;
use App\Support\DberpFilamentInventoryVoidActionConfigurator;
use App\Support\Print\Documents\StocktakeOrderPrintPresenter;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;

class EditDberpStocktakeOrder extends EditRecord
{
    protected static string $resource = DberpStocktakeOrderResource::class;

    /**
     * @param  array<string, mixed>  $data
     * @return array<string, mixed>
     */
    protected function mutateFormDataBeforeSave(array $data): array
    {
        $data = DberpStocktakeOrder::normalizeScopeFormData($data);

        $order = $this->getRecord();
        if ($order instanceof DberpStocktakeOrder) {
            $order->fill($data);
            $order->assertScopeFieldsForSave();
        }

        return $data;
    }

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