<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\Menu\CreateMenuAction;
use App\Actions\Menu\DeleteMenuAction;
use App\Actions\Menu\UpdateMenuAction;
use App\DTOs\MenuData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Menu\ReorderMenuRequest;
use App\Http\Requests\Menu\StoreMenuRequest;
use App\Http\Requests\Menu\UpdateMenuRequest;
use App\Http\Resources\MenuResource;
use App\Models\Menu;
use App\Repositories\Contracts\MenuRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
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/admin/menus',
        summary: 'Menü öğelerini listele (Admin)',
        tags: ['Admin - Menus'],
        security: [['sanctum' => []]],
        parameters: [
            new OA\Parameter(name: 'location', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
            new OA\Parameter(name: 'per_page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 50)),
        ],
        responses: [new OA\Response(response: 200, description: 'Menü listesi')],
    )]
    public function index(Request $request): AnonymousResourceCollection
    {
        $menus = $this->repository->paginate(
            $request->only(['location']),
            $request->integer('per_page', 50),
        );

        return MenuResource::collection($menus);
    }

    #[OA\Post(
        path: '/api/admin/menus',
        summary: 'Yeni menü öğesi oluştur',
        tags: ['Admin - Menus'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['title', 'url', 'location'],
            properties: [
                new OA\Property(property: 'title', type: 'string'),
                new OA\Property(property: 'url', type: 'string'),
                new OA\Property(property: 'location', type: 'string'),
                new OA\Property(property: 'parent_id', type: 'integer'),
                new OA\Property(property: 'target', type: 'string'),
                new OA\Property(property: 'icon', type: 'string'),
                new OA\Property(property: 'is_active', type: 'boolean'),
                new OA\Property(property: 'sort_order', type: 'integer'),
            ],
        )),
        responses: [
            new OA\Response(response: 201, description: 'Menü oluşturuldu'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function store(StoreMenuRequest $request, CreateMenuAction $action): JsonResponse
    {
        $dto = MenuData::fromArray($request->validated());
        $menu = $action->execute($dto);

        return response()->json(new MenuResource($menu), 201);
    }

    #[OA\Get(
        path: '/api/admin/menus/{menu}',
        summary: 'Menü detayı',
        tags: ['Admin - Menus'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'menu', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Menü detayı')],
    )]
    public function show(Menu $menu): JsonResponse
    {
        return response()->json(new MenuResource($menu->load('children')));
    }

    #[OA\Put(
        path: '/api/admin/menus/{menu}',
        summary: 'Menü güncelle',
        tags: ['Admin - Menus'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'menu', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            properties: [
                new OA\Property(property: 'title', type: 'string'),
                new OA\Property(property: 'url', type: 'string'),
                new OA\Property(property: 'location', type: 'string'),
                new OA\Property(property: 'parent_id', type: 'integer'),
                new OA\Property(property: 'is_active', type: 'boolean'),
                new OA\Property(property: 'sort_order', type: 'integer'),
            ],
        )),
        responses: [
            new OA\Response(response: 200, description: 'Menü güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateMenuRequest $request, Menu $menu, UpdateMenuAction $action): JsonResponse
    {
        $dto = MenuData::fromArray(array_merge($menu->toArray(), $request->validated()));
        $menu = $action->execute($menu, $dto);

        return response()->json(new MenuResource($menu->load('children')));
    }

    #[OA\Delete(
        path: '/api/admin/menus/{menu}',
        summary: 'Menü sil',
        tags: ['Admin - Menus'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'menu', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Menü silindi')],
    )]
    public function destroy(Menu $menu, DeleteMenuAction $action): JsonResponse
    {
        $action->execute($menu);

        return response()->json(['message' => 'Menü silindi.']);
    }

    #[OA\Post(
        path: '/api/admin/menus/reorder',
        summary: 'Menü sıralamasını güncelle',
        tags: ['Admin - Menus'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['items'],
            properties: [
                new OA\Property(property: 'items', type: 'array', items: new OA\Items(
                    properties: [
                        new OA\Property(property: 'id', type: 'integer'),
                        new OA\Property(property: 'order_index', type: 'integer'),
                        new OA\Property(property: 'parent_id', type: 'integer', nullable: true),
                    ],
                )),
            ],
        )),
        responses: [new OA\Response(response: 200, description: 'Sıralama güncellendi')],
    )]
    public function reorder(ReorderMenuRequest $request): JsonResponse
    {
        $this->repository->reorder($request->validated('items'));

        return response()->json(['message' => 'Menü sıralaması güncellendi.']);
    }
}
