<?php

namespace Functional\Tracking\Exceptions;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;

/**
 * Levée quand on tente de logger un set ou de compléter une séance déjà terminée
 * (status completed ou abandoned).
 */
class WorkoutSessionAlreadyClosedException extends RuntimeException
{
    public function __construct(
        public readonly int $workoutSessionId,
    ) {
        parent::__construct("Workout session {$workoutSessionId} is already closed.");
    }

    /** Rendu HTTP au format Laravel standard ({message}), en 422 (règle métier violée). */
    public function render(Request $request): JsonResponse
    {
        $locale = $request->user()?->locale ?? 'fr';

        return response()->json(
            ['message' => __('tracking.session_already_closed', [], $locale)],
            Response::HTTP_UNPROCESSABLE_ENTITY,
        );
    }
}
