<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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\PluginIntegration\BarcodeLabelFilamentBridge;
use App\Support\PluginIntegration\LightApprovalDocumentGuardBridge;
use App\Support\PluginIntegration\LightApprovalFilamentBridge;
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 [
            ...($this->record instanceof DberpOtherStockInOrder
                ? LightApprovalFilamentBridge::inventoryAdjustmentEditHeaderActions($this->record)
                : []),
            ...BarcodeLabelFilamentBridge::otherStockInHeaderActions(
                fn (DberpOtherStockInOrder $record): bool => DberpOtherStockInOrderResource::canView($record),
            ),
            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)
                ->disabled(fn (DberpOtherStockInOrder $record): bool => ! LightApprovalDocumentGuardBridge::canConfirm(
                    LightApprovalDocumentGuardBridge::ADAPTER_INVENTORY_ADJUSTMENT,
                    $record,
                ))
                ->tooltip(fn (DberpOtherStockInOrder $record): ?string => LightApprovalDocumentGuardBridge::confirmBlockReason(
                    LightApprovalDocumentGuardBridge::ADAPTER_INVENTORY_ADJUSTMENT,
                    $record,
                ))
                ->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),
        ];
    }
}
