<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpSettlementAdjustments\Pages;

use App\Business\Settlement\Enums\SettlementAdjustmentStatus;
use App\Business\Settlement\SettlementAdjustmentService;
use App\Filament\Resources\DberpSettlementAdjustments\DberpSettlementAdjustmentResource;
use App\Filament\Resources\Pages\EditRecord;
use App\Models\DberpSettlementAdjustment;
use App\Services\DberpPermissionService;
use App\Support\DberpFilamentSettlementVoidActionConfigurator;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Auth;

class EditDberpSettlementAdjustment extends EditRecord
{
    protected static string $resource = DberpSettlementAdjustmentResource::class;

    /**
     * @param  array<string, mixed>  $data
     * @return array<string, mixed>
     */
    protected function mutateFormDataBeforeSave(array $data): array
    {
        $data['counterparty_type'] = ($data['settlement_side'] ?? 'receivable') === 'payable' ? 'supplier' : 'customer';

        if (($data['settlement_side'] ?? 'receivable') === 'payable') {
            $data['receivable_record_id'] = null;
        } else {
            $data['payable_record_id'] = null;
        }

        return $data;
    }

    protected function getHeaderActions(): array
    {
        return [
            Action::make('apply')
                ->label(__('dberp.actions.apply_adjustment'))
                ->requiresConfirmation()
                ->modalDescription(__('dberp.hints.settlement_adjustment_apply'))
                ->visible(fn (DberpSettlementAdjustment $record): bool => $record->status === SettlementAdjustmentStatus::Draft->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CREATE_SETTLEMENT_ADJUSTMENT))
                ->action(function (): void {
                    app(SettlementAdjustmentService::class)->apply($this->record);
                    $this->record->refresh();
                }),
            DberpFilamentSettlementVoidActionConfigurator::configureEffectiveSettlementVoidAction(
                Action::make('void')
                    ->label(__('dberp.actions.void_settlement'))
                    ->requiresConfirmation()
                    ->modalHeading(fn (): string => (string) __('dberp.hints.settlement_adjustment_void_heading'))
                    ->modalDescription(__('dberp.hints.settlement_adjustment_void_body'))
                    ->visible(fn (DberpSettlementAdjustment $record): bool => $record->status === SettlementAdjustmentStatus::Effective->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CREATE_SETTLEMENT_ADJUSTMENT))
                    ->action(function (): void {
                        app(SettlementAdjustmentService::class)->void($this->record);
                        $this->record->refresh();
                        Notification::make()
                            ->title((string) __('dberp.notifications.settlement_void_done'))
                            ->success()
                            ->send();
                    }),
            ),
            DeleteAction::make()
                ->visible(fn (DberpSettlementAdjustment $record): bool => $record->status === SettlementAdjustmentStatus::Draft->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CREATE_SETTLEMENT_ADJUSTMENT)),
        ];
    }
}
