<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpSettlementStatements\Pages;

use App\Business\Settlement\Enums\SettlementStatementStatus;
use App\Business\Settlement\SettlementStatementService;
use App\Filament\Resources\DberpSettlementStatements\DberpSettlementStatementResource;
use App\Filament\Resources\Pages\EditRecord;
use App\Filament\Support\DberpExportCsvFilamentAction;
use App\Models\DberpSettlementStatement;
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 EditDberpSettlementStatement extends EditRecord
{
    protected static string $resource = DberpSettlementStatementResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Action::make('confirm')
                ->label(__('dberp.actions.confirm_statement'))
                ->requiresConfirmation()
                ->modalDescription(__('dberp.hints.settlement_statement_confirm'))
                ->visible(fn (DberpSettlementStatement $record): bool => $record->statement_status === SettlementStatementStatus::Draft->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CONFIRM_SETTLEMENT_STATEMENT))
                ->action(function (): void {
                    app(SettlementStatementService::class)->confirm($this->record);
                    $this->record->refresh();
                }),
            DberpExportCsvFilamentAction::configure(
                Action::make('export')
                    ->label(__('dberp.actions.export'))
                    ->url(fn (): string => route('dberp.settlement-statements.export', ['statement' => $this->record]))
                    ->visible(fn (DberpSettlementStatement $record): bool => $record->statement_status !== SettlementStatementStatus::Voided->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::EXPORT)),
            ),
            DberpFilamentSettlementVoidActionConfigurator::configureEffectiveSettlementVoidAction(
                Action::make('void')
                    ->label(fn (DberpSettlementStatement $record): string => $record->statement_status === SettlementStatementStatus::Confirmed->value
                        ? (string) __('dberp.actions.void_settlement')
                        : (string) __('dberp.actions.void'))
                    ->requiresConfirmation()
                    ->modalHeading(fn (): string => (string) __('dberp.hints.settlement_statement_void_heading'))
                    ->modalDescription(fn (DberpSettlementStatement $record): string => $record->statement_status === SettlementStatementStatus::Confirmed->value
                        ? (string) __('dberp.hints.settlement_statement_void_confirmed_body')
                        : (string) __('dberp.hints.settlement_statement_void_draft_body'))
                    ->visible(fn (DberpSettlementStatement $record): bool => $record->statement_status !== SettlementStatementStatus::Voided->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CONFIRM_SETTLEMENT_STATEMENT))
                    ->action(function (): void {
                        app(SettlementStatementService::class)->void($this->record);
                        $this->record->refresh();
                        Notification::make()
                            ->title((string) __('dberp.notifications.settlement_void_done'))
                            ->success()
                            ->send();
                    }),
            ),
            DeleteAction::make()
                ->visible(fn (DberpSettlementStatement $record): bool => $record->statement_status === SettlementStatementStatus::Draft->value && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CONFIRM_SETTLEMENT_STATEMENT)),
        ];
    }
}
