<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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();
                }),
        ];
    }
}