<?php

declare(strict_types=1);

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

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