<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use OpenApi\Attributes as OA;

class PreviewController extends Controller
{
    #[OA\Get(
        path: '/api/v1/preview/{token}',
        summary: 'Önizleme verisini getir (public)',
        tags: ['Preview'],
        parameters: [new OA\Parameter(name: 'token', in: 'path', required: true, schema: new OA\Schema(type: 'string'))],
        responses: [
            new OA\Response(response: 200, description: 'Önizleme verisi'),
            new OA\Response(response: 404, description: 'Önizleme bulunamadı veya süresi dolmuş'),
        ],
    )]
    public function show(string $token): JsonResponse
    {
        $data = Cache::get("preview_{$token}");

        if (! $data) {
            return response()->json(['message' => 'Önizleme bulunamadı veya süresi dolmuş.'], 404);
        }

        return response()->json(['data' => $data]);
    }
}
