<?php

namespace Functional\Tracking\Http\Controllers;

use Functional\Tracking\Enums\WorkoutSessionStatus;
use Functional\Tracking\Http\Resources\WorkoutSessionResource;
use Functional\Tracking\Models\WorkoutSession;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

/**
 * Historique des séances terminées de l'utilisateur authentifié.
 */
class HistoryController
{
    private const DEFAULT_PER_PAGE = 20;

    /** GET /api/history */
    public function index(Request $request): AnonymousResourceCollection
    {
        $sessions = WorkoutSession::query()
            ->where('user_id', auth()->id())
            ->where('status', WorkoutSessionStatus::Completed)
            ->withCount('setLogs')
            ->orderByDesc('started_at')
            ->paginate((int) $request->query('per_page', self::DEFAULT_PER_PAGE));

        return WorkoutSessionResource::collection($sessions);
    }
}
