<?php

declare(strict_types=1);

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

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