<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;

class EditorUploadController extends Controller
{
    /**
     * Handle image uploads from the Tiptap editor.
     */
    public function uploadImage(Request $request): JsonResponse
    {
        $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', // 2MB Max
        ]);

        if ($request->hasFile('image')) {
            // Store the image in 'public/editor-uploads'
            $path = $request->file('image')->store('editor-uploads', 'public');

            // Return the public URL of the stored image
            return response()->json([
                'url' => Storage::url($path)
            ]);
        }

        return response()->json(['error' => 'No image uploaded.'], 400);
    }
}