<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpInventoryBatches\Pages;

use App\Business\Inventory\Enums\InventoryBatchStatus;
use App\Filament\Resources\DberpInventoryBatches\DberpInventoryBatchResource;
use App\Models\DberpInventoryBatch;
use Filament\Actions\Action;
use App\Filament\Resources\Pages\EditRecord;

class EditDberpInventoryBatch extends EditRecord
{
    protected static string $resource = DberpInventoryBatchResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Action::make('toggle_enabled')
                ->label(fn (DberpInventoryBatch $record): string => $record->is_enabled ? __('dberp.actions.disable') : __('dberp.actions.enable'))
                ->requiresConfirmation()
                ->action(function (): void {
                    /** @var DberpInventoryBatch $record */
                    $record = $this->record;

                    $record->forceFill([
                        'is_enabled' => ! $record->is_enabled,
                        'status' => $record->is_enabled ? InventoryBatchStatus::Disabled->value : InventoryBatchStatus::Active->value,
                    ])->save();

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