<?php

declare(strict_types=1);

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