<?php

namespace App\Http\Controllers\Admin;

use App\Actions\Payments\ConfirmPaymentAction;
use App\Actions\Payments\RejectPaymentAction;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\RejectPaymentProofRequest;
use App\Models\EventPaymentProof;
use App\Queries\EventRegistrationQuery;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use InvalidArgumentException;

class PaymentProofReviewController extends Controller
{
    public function __construct(private EventRegistrationQuery $registrationQuery) {}

    public function index(): View
    {
        $this->authorize('review-payment-proofs');

        return view('admin.payment-proofs.index', [
            'registrations' => $this->registrationQuery->pendingPaymentProofs(),
        ]);
    }

    public function confirm(EventPaymentProof $proof, ConfirmPaymentAction $action): RedirectResponse
    {
        $this->authorize('review-payment-proofs');

        try {
            $action->execute($proof, auth()->user());
        } catch (InvalidArgumentException $e) {
            return back()->withErrors(['proof' => $e->getMessage()]);
        }

        return back()->with('status', __('admin.payment_proofs.approved_success'));
    }

    public function reject(
        RejectPaymentProofRequest $request,
        EventPaymentProof $proof,
        RejectPaymentAction $action,
    ): RedirectResponse {
        $this->authorize('review-payment-proofs');

        try {
            $action->execute($proof, auth()->user(), $request->validated('rejection_reason'));
        } catch (InvalidArgumentException $e) {
            return back()->withErrors(['proof' => $e->getMessage()]);
        }

        return back()->with('status', __('admin.payment_proofs.rejected_success'));
    }
}
