<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpInventoryReplenishmentSuggestions\Pages;

use App\Business\Inventory\Enums\InventoryReplenishmentSuggestionStatus;
use App\Business\Inventory\InventoryReplenishmentSuggestionService;
use App\Business\Purchase\PurchaseRequestGenerationService;
use App\Filament\Resources\DberpInventoryReplenishmentSuggestions\DberpInventoryReplenishmentSuggestionResource;
use App\Filament\Resources\DberpPurchaseRequests\DberpPurchaseRequestResource;
use App\Models\DberpInventoryReplenishmentSuggestion;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use App\Filament\Resources\Pages\EditRecord;

class EditDberpInventoryReplenishmentSuggestion extends EditRecord
{
    protected static string $resource = DberpInventoryReplenishmentSuggestionResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Action::make('reviewed')
                ->label(__('dberp.actions.reviewed'))
                ->requiresConfirmation()
                ->visible(fn (DberpInventoryReplenishmentSuggestion $record): bool => $record->status === InventoryReplenishmentSuggestionStatus::Pending->value)
                ->action(function (): void {
                    app(InventoryReplenishmentSuggestionService::class)->markReviewed($this->record);
                    $this->record->refresh();
                }),
            Action::make('processed')
                ->label(__('dberp.actions.processed'))
                ->requiresConfirmation()
                ->visible(fn (DberpInventoryReplenishmentSuggestion $record): bool => in_array($record->status, [InventoryReplenishmentSuggestionStatus::Pending->value, InventoryReplenishmentSuggestionStatus::Reviewed->value], true))
                ->action(function (): void {
                    app(InventoryReplenishmentSuggestionService::class)->markProcessed($this->record);
                    $this->record->refresh();
                }),
            Action::make('generate_purchase_request')
                ->label(__('dberp.actions.generate_purchase_request'))
                ->requiresConfirmation()
                ->visible(fn (DberpInventoryReplenishmentSuggestion $record): bool => in_array($record->status, [InventoryReplenishmentSuggestionStatus::Pending->value, InventoryReplenishmentSuggestionStatus::Reviewed->value], true))
                ->action(function (): void {
                    $request = app(PurchaseRequestGenerationService::class)->createFromReplenishmentSuggestion($this->record);

                    Notification::make()
                        ->title((string) __('dberp.notifications.saved'))
                        ->body((string) __('dberp.messages.purchase_request_generated_from_replenishment', ['code' => $request->code]))
                        ->success()
                        ->send();

                    $this->redirect(DberpPurchaseRequestResource::getUrl('edit', ['record' => $request]));
                }),
            Action::make('ignored')
                ->label(__('dberp.actions.ignored'))
                ->requiresConfirmation()
                ->visible(fn (DberpInventoryReplenishmentSuggestion $record): bool => $record->status !== InventoryReplenishmentSuggestionStatus::Processed->value)
                ->action(function (): void {
                    app(InventoryReplenishmentSuggestionService::class)->markIgnored($this->record);
                    $this->record->refresh();
                }),
        ];
    }
}