<?php

declare(strict_types=1);

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

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;
    }
}
