<?php

declare(strict_types=1);

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();
    }
}
