<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class WikiEditorUploadController extends Controller
{
    public function __invoke(Request $request): JsonResponse
    {
        abort_unless($request->user()?->can('wiki.criar') || $request->user()?->can('wiki.editar'), 403);

        $dados = $request->validate([
            'arquivo' => ['required', 'image', 'max:5120'],
        ]);

        $caminho = $dados['arquivo']->store('wiki/editor', 'public');

        return response()->json([
            'url' => route('wiki.editor-media', ['path' => $caminho]),
        ]);
    }
}
