<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\GuideCard\CreateGuideCardAction;
use App\Actions\GuideCard\DeleteGuideCardAction;
use App\Actions\GuideCard\UpdateGuideCardAction;
use App\DTOs\GuideCardData;
use App\Http\Controllers\Controller;
use App\Http\Requests\GuideCard\StoreGuideCardRequest;
use App\Http\Requests\GuideCard\UpdateGuideCardRequest;
use App\Http\Resources\GuideCardResource;
use App\Models\GuideCard;
use App\Repositories\Contracts\GuideCardRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class GuideCardController extends Controller
{
    public function __construct(private GuideCardRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/admin/guide-cards',
        summary: 'Rehber kartları listele (Admin)',
        tags: ['Admin - Guide Cards'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'per_page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 15))],
        responses: [new OA\Response(response: 200, description: 'Kart listesi')],
    )]
    public function index(Request $request): AnonymousResourceCollection
    {
        $cards = $this->repository->paginate(
            $request->only([]),
            $request->integer('per_page', 15),
        );

        return GuideCardResource::collection($cards);
    }

    #[OA\Post(
        path: '/api/admin/guide-cards',
        summary: 'Yeni rehber kartı oluştur',
        tags: ['Admin - Guide Cards'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['title', 'description', 'icon'],
            properties: [
                new OA\Property(property: 'title', type: 'string'),
                new OA\Property(property: 'description', type: 'string'),
                new OA\Property(property: 'icon', type: 'string'),
                new OA\Property(property: 'url', 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: 'Kart oluşturuldu'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function store(StoreGuideCardRequest $request, CreateGuideCardAction $action): JsonResponse
    {
        $dto = GuideCardData::fromArray($request->validated());
        $card = $action->execute($dto);

        return response()->json(new GuideCardResource($card), 201);
    }

    #[OA\Get(
        path: '/api/admin/guide-cards/{guideCard}',
        summary: 'Rehber kart detayı',
        tags: ['Admin - Guide Cards'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'guideCard', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Kart detayı')],
    )]
    public function show(GuideCard $guideCard): JsonResponse
    {
        return response()->json(new GuideCardResource($guideCard));
    }

    #[OA\Put(
        path: '/api/admin/guide-cards/{guideCard}',
        summary: 'Rehber kart güncelle',
        tags: ['Admin - Guide Cards'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'guideCard', 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: 'description', type: 'string'),
                new OA\Property(property: 'icon', type: 'string'),
                new OA\Property(property: 'url', type: 'string'),
                new OA\Property(property: 'is_active', type: 'boolean'),
                new OA\Property(property: 'sort_order', type: 'integer'),
            ],
        )),
        responses: [
            new OA\Response(response: 200, description: 'Kart güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateGuideCardRequest $request, GuideCard $guideCard, UpdateGuideCardAction $action): JsonResponse
    {
        $dto = GuideCardData::fromArray(array_merge($guideCard->toArray(), $request->validated()));
        $guideCard = $action->execute($guideCard, $dto);

        return response()->json(new GuideCardResource($guideCard));
    }

    #[OA\Delete(
        path: '/api/admin/guide-cards/{guideCard}',
        summary: 'Rehber kart sil',
        tags: ['Admin - Guide Cards'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'guideCard', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Kart silindi')],
    )]
    public function destroy(GuideCard $guideCard, DeleteGuideCardAction $action): JsonResponse
    {
        $action->execute($guideCard);

        return response()->json(['message' => 'Rehber kartı silindi.']);
    }
}
