<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\PresignedUrlRequest;
use App\Services\S3PresignedService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;

class PresignedUrlController extends Controller
{
    public function __construct(
        private S3PresignedService $service
    ) {}

    public function store(PresignedUrlRequest $request): JsonResponse
    {
        $validated = $request->validated();

        $result = $this->service->createUploadUrl(
            $validated['file_name'],
            $validated['mime_type'],
            $validated['folder'] ?? null,
        );

        return response()->json($result);
    }

    public function show(string $key): RedirectResponse
    {
        $minutes = (int) request('minutes', 60);
        $url = $this->service->getTemporaryUrl($key, $minutes);

        return redirect()->away($url);
    }
}
