<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpPurchaseRequests\Pages;

use App\Business\Purchase\Enums\PurchaseRequestStatus;
use App\Business\Purchase\PurchaseRequestService;
use App\Filament\Resources\DberpPurchaseRequests\Concerns\ProvidesDberpPurchaseRequestCollaborationHeaderActions;
use App\Filament\Resources\DberpPurchaseRequests\DberpPurchaseRequestResource;
use App\Filament\Resources\Pages\EditRecord;
use App\Models\DberpPurchaseRequest;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Illuminate\Database\Eloquent\Model;

class EditDberpPurchaseRequest extends EditRecord
{
    use ProvidesDberpPurchaseRequestCollaborationHeaderActions;

    protected static string $resource = DberpPurchaseRequestResource::class;

    protected function handleRecordUpdate(Model $record, array $data): Model
    {
        return app(PurchaseRequestService::class)->updateRequest($record, $data);
    }

    protected function getHeaderActions(): array
    {
        return [
            Action::make('submit')
                ->label(__('dberp.actions.submit'))
                ->requiresConfirmation()
                ->modalDescription(__('dberp.hints.purchase_request_submit'))
                ->visible(fn (DberpPurchaseRequest $record): bool => $record->status === PurchaseRequestStatus::Draft->value)
                ->action(function (): void {
                    app(PurchaseRequestService::class)->submit($this->record);
                    $this->record->refresh();
                }),
            ...$this->purchaseRequestCollaborationHeaderActions(),
            Action::make('close')
                ->label(__('dberp.actions.close'))
                ->requiresConfirmation()
                ->modalDescription(__('dberp.hints.purchase_request_close'))
                ->visible(fn (DberpPurchaseRequest $record): bool => in_array($record->status, [PurchaseRequestStatus::Pending->value, PurchaseRequestStatus::PartiallyConverted->value, PurchaseRequestStatus::Converted->value], true))
                ->action(function (): void {
                    app(PurchaseRequestService::class)->close($this->record);
                    $this->record->refresh();
                }),
            Action::make('void')
                ->label(__('dberp.actions.void'))
                ->requiresConfirmation()
                ->modalHeading(fn (): string => (string) __('dberp.hints.purchase_request_void_heading'))
                ->modalDescription(__('dberp.hints.purchase_request_void'))
                ->visible(fn (DberpPurchaseRequest $record): bool => in_array($record->status, [PurchaseRequestStatus::Draft->value, PurchaseRequestStatus::Pending->value], true))
                ->action(function (): void {
                    app(PurchaseRequestService::class)->void($this->record);
                    $this->record->refresh();
                }),
            DeleteAction::make()
                ->visible(fn (DberpPurchaseRequest $record): bool => $record->status === PurchaseRequestStatus::Draft->value),
        ];
    }

    protected function getPurchaseRequestCollaborationRecord(): DberpPurchaseRequest
    {
        return $this->record;
    }
}
