<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpInventoryReplenishmentSuggestions\Pages;

use App\Business\Inventory\InventoryReplenishmentSuggestionService;
use App\Filament\Resources\DberpInventoryReplenishmentSuggestions\DberpInventoryReplenishmentSuggestionResource;
use App\Services\DberpPermissionService;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\Auth;

class ListDberpInventoryReplenishmentSuggestions extends ListRecords
{
    protected static string $resource = DberpInventoryReplenishmentSuggestionResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Action::make('generate')
                ->label(__('dberp.actions.generate_replenishment_suggestions'))
                ->visible(fn (): bool => app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::HANDLE_REPLENISHMENT_SUGGESTION))
                ->action(function (): void {
                    $count = app(InventoryReplenishmentSuggestionService::class)->generateForAllActiveRules();

                    Notification::make()
                        ->title(__('dberp.notifications.saved'))
                        ->body(__('dberp.hints.replenishment_generate_result', ['count' => $count]))
                        ->success()
                        ->send();
                }),
        ];
    }
}