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