<?php

declare(strict_types=1);

namespace App\Http\Controllers\Academy;

use App\Http\Controllers\Controller;
use App\Models\AcademyLesson;
use App\Services\Academy\AcademyAccessService;
use App\Services\Academy\AcademyProgressService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

final class AcademyProgressController extends Controller
{
    public function __construct(
        private readonly AcademyAccessService $access,
        private readonly AcademyProgressService $progress,
    ) {
    }

    public function complete(Request $request, AcademyLesson $lesson): JsonResponse
    {
        abort_unless((bool) config('academy.enabled', true), 404);
        abort_unless($this->access->canAccessLesson($request->user(), $lesson), 403);

        $record = $this->progress->markLessonComplete($request->user(), $lesson);

        return response()->json([
            'ok' => true,
            'completed' => true,
            'completed_at' => $record->completed_at?->toISOString(),
        ]);
    }
}