<?php

namespace App\Http\Controllers\Athlete;

use App\Http\Controllers\Controller;
use App\Http\Requests\Athlete\SubmitPaymentRequest;
use App\Models\CoachRequest;
use App\Services\PaymentService;
use Illuminate\Http\RedirectResponse;

class PaymentController extends Controller
{
    public function __construct(private readonly PaymentService $paymentService)
    {
    }

    public function store(SubmitPaymentRequest $request, CoachRequest $coachRequest): RedirectResponse
    {
        $this->authorize('submit', $coachRequest);

        try {
            $this->paymentService->submit(
                $coachRequest,
                $request->file('receipt'),
                $request->filled('amount') ? (float) $request->input('amount') : null,
            );
        } catch (\RuntimeException $e) {
            return back()->withErrors(['receipt' => $e->getMessage()]);
        }

        return back()->with('status', 'رسید پرداخت ارسال شد و در انتظار بررسی مربی است.');
    }
}
