<?php

declare(strict_types=1);

namespace App\Http\Controllers\Academy;

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

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

    public function store(Request $request, AcademyPromptTemplate $prompt): JsonResponse
    {
        abort_unless((bool) config('academy.enabled', true), 404);
        abort_unless($this->access->canAccessPrompt($request->user(), $prompt), 403);

        $this->progress->savePrompt($request->user(), $prompt);

        return response()->json([
            'ok' => true,
            'saved' => true,
        ]);
    }

    public function destroy(Request $request, AcademyPromptTemplate $prompt): JsonResponse
    {
        abort_unless((bool) config('academy.enabled', true), 404);
        abort_unless($this->access->canAccessPrompt($request->user(), $prompt), 403);

        $this->progress->unsavePrompt($request->user(), $prompt);

        return response()->json([
            'ok' => true,
            'saved' => false,
        ]);
    }
}