<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Resources\PageResource;
use App\Repositories\Contracts\PageRepositoryInterface;
use Illuminate\Http\JsonResponse;
use OpenApi\Attributes as OA;

class PageController extends Controller
{
    public function __construct(private PageRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/v1/pages/{slug}',
        summary: 'Sayfa detayı',
        tags: ['Pages'],
        parameters: [new OA\Parameter(name: 'slug', in: 'path', required: true, schema: new OA\Schema(type: 'string'))],
        responses: [
            new OA\Response(response: 200, description: 'Sayfa detayı'),
            new OA\Response(response: 404, description: 'Sayfa bulunamadı'),
        ],
    )]
    public function show(string $slug): JsonResponse
    {
        $page = $this->repository->findBySlug($slug);

        if (! $page || ! $page->is_active) {
            return response()->json(['message' => 'Sayfa bulunamadı.'], 404);
        }

        return response()->json(new PageResource($page));
    }
}
