<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpSettlementWriteOffs\Pages;

use App\Business\Settlement\Enums\SettlementWriteOffStatus;
use App\Business\Settlement\SettlementWriteOffService;
use App\Filament\Resources\DberpSettlementWriteOffs\DberpSettlementWriteOffResource;
use App\Filament\Resources\Pages\EditRecord;
use App\Models\DberpSettlementWriteOff;
use App\Services\DberpPermissionService;
use Filament\Actions\Action;
use Illuminate\Support\Facades\Auth;

class EditDberpSettlementWriteOff extends EditRecord
{
    protected static string $resource = DberpSettlementWriteOffResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Action::make('reverse')
                ->label(__('dberp.actions.reverse_write_off'))
                ->requiresConfirmation()
                ->modalDescription(__('dberp.hints.settlement_write_off_reverse'))
                ->visible(fn (DberpSettlementWriteOff $record): bool => in_array($record->write_off_status, [SettlementWriteOffStatus::Confirmed->value, SettlementWriteOffStatus::PartiallyReversed->value], true) && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::REVERSE_WRITE_OFF))
                ->action(function (): void {
                    app(SettlementWriteOffService::class)->reverse($this->record);
                    $this->record->refresh();
                }),
        ];
    }
}
