<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class UploadController extends Controller
{
    public function image(Request $request): JsonResponse
    {
        $request->validate([
            'file' => 'required|image|max:5120', // 5MB
        ]);

        $file = $request->file('file');
        $name = Str::uuid() . '.' . $file->getClientOriginalExtension();
        $file->move(public_path('uploads'), $name);

        return response()->json(['url' => url('uploads/' . $name)]);
    }

    public function pdf(Request $request): JsonResponse
    {
        $request->validate([
            'file' => 'required|file|mimes:pdf|max:10240', // 10MB
        ]);

        $file = $request->file('file');
        $name = Str::uuid() . '.pdf';
        $file->move(public_path('uploads/pdfs'), $name);

        return response()->json(['url' => url('uploads/pdfs/' . $name)]);
    }
}
