<?php

namespace App\Http\Controllers\Api\V1;

use App\Enums\MenuLocation;
use App\Http\Controllers\Controller;
use App\Http\Resources\MenuResource;
use App\Repositories\Contracts\MenuRepositoryInterface;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class MenuController extends Controller
{
    public function __construct(private MenuRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/v1/menus/{location}',
        summary: 'Konuma göre menü öğelerini getir',
        tags: ['Menus'],
        parameters: [new OA\Parameter(name: 'location', in: 'path', required: true, schema: new OA\Schema(type: 'string'), description: 'header, footer, mobile')],
        responses: [
            new OA\Response(response: 200, description: 'Menü listesi'),
            new OA\Response(response: 404, description: 'Menü konumu bulunamadı'),
        ],
    )]
    public function index(string $location): AnonymousResourceCollection
    {
        $menuLocation = MenuLocation::tryFrom($location);

        if (! $menuLocation) {
            abort(404, 'Menü konumu bulunamadı.');
        }

        return MenuResource::collection($this->repository->getByLocation($menuLocation));
    }
}
