<?php

declare(strict_types=1);

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

namespace App\Filament\Resources\DberpStocktakeDifferences\Pages;

use App\Business\Inventory\Enums\StocktakeDifferenceStatus;
use App\Business\Inventory\StocktakeDifferenceService;
use App\Filament\Resources\DberpStocktakeDifferences\DberpStocktakeDifferenceResource;
use App\Models\DberpStocktakeDifference;
use App\Services\DberpPermissionService;
use Filament\Actions\Action;
use App\Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Auth;

class EditDberpStocktakeDifference extends EditRecord
{
    protected static string $resource = DberpStocktakeDifferenceResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Action::make('reviewed')
                ->label(__('dberp.actions.reviewed'))
                ->requiresConfirmation()
                ->visible(fn (DberpStocktakeDifference $record): bool => $record->status === StocktakeDifferenceStatus::Pending->value
                    && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::HANDLE_STOCKTAKE_DIFFERENCE))
                ->action(function (): void {
                    app(StocktakeDifferenceService::class)->markReviewed(
                        $this->record,
                        (string) ($this->data['resolution_remark'] ?? ''),
                    );
                    $this->record->refresh();
                }),
            Action::make('confirmDifference')
                ->label(__('dberp.actions.confirm_difference'))
                ->requiresConfirmation()
                ->visible(fn (DberpStocktakeDifference $record): bool => in_array($record->status, [
                    StocktakeDifferenceStatus::Pending->value,
                    StocktakeDifferenceStatus::Reviewed->value,
                ], true) && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::CONFIRM_STOCKTAKE_DIFFERENCE))
                ->action(function (): void {
                    app(StocktakeDifferenceService::class)->markResolved(
                        $this->record,
                        (string) ($this->data['resolution_remark'] ?? ''),
                        (string) ($this->data['resolution_type'] ?? 'confirmed'),
                    );
                    $this->record->refresh();
                }),
            Action::make('ignored')
                ->label(__('dberp.actions.ignored'))
                ->requiresConfirmation()
                ->visible(fn (DberpStocktakeDifference $record): bool => $record->status !== StocktakeDifferenceStatus::Resolved->value
                    && app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::HANDLE_STOCKTAKE_DIFFERENCE))
                ->action(function (): void {
                    app(StocktakeDifferenceService::class)->markIgnored(
                        $this->record,
                        (string) ($this->data['resolution_remark'] ?? ''),
                    );
                    $this->record->refresh();
                }),
        ];
    }
}