<?php

namespace App\Http\Controllers\Coach;

use App\Http\Controllers\Controller;
use App\Http\Requests\Coach\RejectReviewRequest;
use App\Models\Review;
use App\Services\ReviewService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

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

    public function index(): View
    {
        $coach = auth()->user()->coach;

        $reviews = Review::query()
            ->whereHas('coachRequest', fn ($q) => $q->where('coach_id', $coach->id))
            ->with('coachRequest.athlete.user')
            ->latest()
            ->get();

        return view('coach.reviews.index', compact('reviews'));
    }

    public function approve(Review $review): RedirectResponse
    {
        $this->authorize('coachModerate', $review);

        $this->reviewService->coachApprove($review);

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

    public function reject(RejectReviewRequest $request, Review $review): RedirectResponse
    {
        $this->authorize('coachModerate', $review);

        $this->reviewService->coachReject($review, $request->string('reason')->toString());

        return back()->with('status', 'نظر رد شد.');
    }
}
