<?php

declare(strict_types=1);

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

namespace App\Support;

use App\Business\Inventory\InventoryRuleService;
use Filament\Actions\Action;
use Filament\Notifications\Notification;

final class DberpFilamentInventoryVoidActionConfigurator
{
    public static function inventoryVoidAllowed(): bool
    {
        return app(InventoryRuleService::class)->allowsEffectiveInventoryVoid();
    }

    public static function inventoryRollbackAllowed(): bool
    {
        return app(InventoryRuleService::class)->allowVoidRollback();
    }

    public static function inventoryMarkOnlyVoid(): bool
    {
        return app(InventoryRuleService::class)->allowsVoidMarkOnly();
    }

    public static function configureEffectiveInventoryVoidAction(Action $action): Action
    {
        return $action
            ->label(fn (): string => self::inventoryRollbackAllowed()
                ? (string) __('dberp.actions.revoke_effective')
                : (string) __('dberp.actions.void_effective_mark_only'))
            ->modalHeading(fn (): string => match (true) {
                self::inventoryRollbackAllowed() => (string) __('dberp.hints.inventory_revoke_effective_heading'),
                self::inventoryMarkOnlyVoid() => (string) __('dberp.hints.inventory_void_mark_only_heading'),
                default => (string) __('dberp.hints.inventory_void_policy_action_heading'),
            })
            ->modalDescription(fn (): string => match (true) {
                self::inventoryRollbackAllowed() => (string) __('dberp.hints.inventory_revoke_effective_body'),
                self::inventoryMarkOnlyVoid() => (string) __('dberp.hints.inventory_void_mark_only_body'),
                default => (string) __('dberp.hints.inventory_void_policy_blocked_ui'),
            })
            ->disabled(fn (): bool => ! self::inventoryVoidAllowed())
            ->tooltip(fn (): ?string => self::inventoryVoidAllowed()
                ? null
                : (string) __('dberp.hints.inventory_void_policy_disabled_reason'));
    }

    public static function notifyInventoryRevokeSuccess(): void
    {
        Notification::make()
            ->title((string) __('dberp.notifications.inventory_revoke_effective_done'))
            ->success()
            ->send();
    }
}
