<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpReceiptOrders\Pages;

use App\Business\Settlement\Enums\SettlementDocumentStatus;
use App\Business\Settlement\ReceiptService;
use App\Filament\Resources\DberpReceiptOrders\DberpReceiptOrderResource;
use App\Filament\Resources\Pages\EditRecord;
use App\Filament\Support\DberpPrintFilamentAction;
use App\Models\DberpReceiptOrder;
use App\Services\DberpPermissionService;
use App\Support\DberpFilamentSettlementVoidActionConfigurator;
use App\Support\Print\Documents\ReceiptOrderPrintPresenter;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Auth;

class EditDberpReceiptOrder extends EditRecord
{
    protected static string $resource = DberpReceiptOrderResource::class;

    /**
     * @param  array<string, mixed>  $data
     * @return array<string, mixed>
     */
    protected function mutateFormDataBeforeSave(array $data): array
    {
        $record = $this->getRecord();

        if (! $record instanceof DberpReceiptOrder) {
            return $data;
        }

        if ($record->status === SettlementDocumentStatus::Draft->value) {
            return $data;
        }

        if ($record->status === SettlementDocumentStatus::Submitted->value) {
            return [
                'attachment_paths' => $data['attachment_paths'] ?? $record->attachment_paths,
            ];
        }

        return $data;
    }

    protected function getHeaderActions(): array
    {
        return [
            DberpPrintFilamentAction::forDocument(
                ReceiptOrderPrintPresenter::DOCUMENT_KEY,
                fn (DberpReceiptOrder $record): bool => app(DberpPermissionService::class)->allows(
                    auth()->user(),
                    DberpPermissionService::VIEW_RECEIPT_ORDER,
                ),
            ),
            Action::make('submit')
                ->label(__('dberp.actions.submit_receipt'))
                ->requiresConfirmation()
                ->modalDescription(__('dberp.hints.receipt_order_submit'))
                ->visible(fn (DberpReceiptOrder $record): bool => $record->status === SettlementDocumentStatus::Draft->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CREATE_RECEIPT_ORDER))
                ->action(function (): void {
                    app(ReceiptService::class)->submit($this->record);
                    $this->record->refresh();
                }),
            Action::make('confirm')
                ->label(__('dberp.actions.confirm_receipt'))
                ->requiresConfirmation()
                ->modalDescription(__('dberp.hints.receipt_order_confirm'))
                ->visible(fn (DberpReceiptOrder $record): bool => in_array($record->status, [SettlementDocumentStatus::Draft->value, SettlementDocumentStatus::Submitted->value], true) && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CONFIRM_RECEIPT_PAYMENT))
                ->action(function (): void {
                    app(ReceiptService::class)->confirm($this->record);
                    $this->record->refresh();
                }),
            DberpFilamentSettlementVoidActionConfigurator::configureEffectiveSettlementVoidAction(
                Action::make('void')
                    ->label(__('dberp.actions.void_settlement'))
                    ->requiresConfirmation()
                    ->modalHeading(fn (): string => (string) __('dberp.hints.settlement_void_effective_heading'))
                    ->modalDescription(__('dberp.hints.settlement_void_effective_body'))
                    ->visible(fn (DberpReceiptOrder $record): bool => $record->status === SettlementDocumentStatus::Effective->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CONFIRM_RECEIPT_PAYMENT))
                    ->action(function (): void {
                        app(ReceiptService::class)->void($this->record);
                        $this->record->refresh();
                        Notification::make()
                            ->title((string) __('dberp.notifications.settlement_void_done'))
                            ->success()
                            ->send();
                    }),
            ),
            DeleteAction::make()
                ->visible(fn (DberpReceiptOrder $record): bool => $record->status === SettlementDocumentStatus::Draft->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CREATE_RECEIPT_ORDER)),
        ];
    }
}
