<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpInventorySerialNumbers\Concerns;

use App\Business\Inventory\InventorySerialNumberGovernanceService;
use App\Filament\Resources\DberpInventorySerialNumbers\DberpInventorySerialNumberResource;
use App\Models\DberpInventorySerialNumber;
use Filament\Actions\Action;

trait InteractsWithInventorySerialNumberGovernanceToggle
{
    protected function makeToggleEnabledGovernanceAction(): Action
    {
        return Action::make('toggle_enabled')
            ->label(fn (DberpInventorySerialNumber $record): string => $record->is_enabled
                ? (string) __('dberp.actions.disable')
                : (string) __('dberp.actions.enable'))
            ->visible(fn (DberpInventorySerialNumber $record): bool => DberpInventorySerialNumberResource::canToggleEnabled($record)
                && app(InventorySerialNumberGovernanceService::class)->canToggleEnabledState($record))
            ->requiresConfirmation()
            ->modalHeading(fn (DberpInventorySerialNumber $record): string => $record->is_enabled
                ? (string) __('dberp.actions.disable')
                : (string) __('dberp.actions.enable'))
            ->modalDescription(fn (DberpInventorySerialNumber $record): string => $record->is_enabled
                ? (string) __('dberp.hints.inventory_serial_number_disable_confirm')
                : (string) __('dberp.hints.inventory_serial_number_enable_confirm'))
            ->action(function (): void {
                /** @var DberpInventorySerialNumber $record */
                $record = $this->record;

                app(InventorySerialNumberGovernanceService::class)->toggleEnabled($record);

                $this->record->refresh();
            });
    }
}
