<?php

declare(strict_types=1);

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

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