<?php

namespace App\Http\Controllers\Coach;

use App\Http\Controllers\Controller;
use App\Http\Requests\Coach\StoreCoachAssessmentRequest;
use App\Models\Athlete;
use App\Models\AthleteAssessment;
use App\Models\Goal;
use App\Services\Athlete\AssessmentService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class AssessmentController extends Controller
{
    public function __construct(private readonly AssessmentService $assessmentService)
    {
    }

    public function create(Athlete $athlete): View
    {
        $this->authorize('create', [AthleteAssessment::class, $athlete]);

        $goals = Goal::query()->active()->ordered()->get();

        return view('coach.assessments.create', compact('athlete', 'goals'));
    }

    public function store(StoreCoachAssessmentRequest $request, Athlete $athlete): RedirectResponse
    {
        $this->authorize('create', [AthleteAssessment::class, $athlete]);

        $isInitial = ! $athlete->hasCompletedInitialAssessment();

        $data = $request->validated();
        $data['coach_id'] = $request->user()->coach->id;

        $assessment = $this->assessmentService->createAssessment(
            $athlete,
            $data,
            $request->user()->id,
            isInitial: $isInitial,
        );

        return redirect()->route('coach.assessments.show', $assessment)->with('status', 'ارزیابی جدید برای ورزشکار ثبت شد.');
    }
}
