<?php

namespace App\Http\Controllers\Athlete;

use App\Http\Controllers\Controller;
use App\Http\Requests\Athlete\SubmitReviewRequest;
use App\Models\CoachRequest;
use App\Services\ReviewService;
use Illuminate\Http\RedirectResponse;

class ReviewController extends Controller
{
    public function __construct(private readonly ReviewService $reviewService)
    {
    }

    public function store(SubmitReviewRequest $request, CoachRequest $coachRequest): RedirectResponse
    {
        $this->authorize('create', [\App\Models\Review::class, $coachRequest]);

        if ($coachRequest->review) {
            return back()->withErrors(['review' => 'شما قبلاً برای این همکاری نظر ثبت کرده‌اید.']);
        }

        $this->reviewService->submit($coachRequest, (int) $request->input('rating'), $request->input('comment'));

        return back()->with('status', 'نظر شما ثبت شد و پس از تأیید مدیریت در پروفایل مربی نمایش داده می‌شود.');
    }
}
