<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\HeroSlide\CreateHeroSlideAction;
use App\Actions\HeroSlide\DeleteHeroSlideAction;
use App\Actions\HeroSlide\UpdateHeroSlideAction;
use App\DTOs\HeroSlideData;
use App\Http\Controllers\Controller;
use App\Http\Requests\HeroSlide\StoreHeroSlideRequest;
use App\Http\Requests\HeroSlide\UpdateHeroSlideRequest;
use App\Http\Resources\HeroSlideResource;
use App\Models\HeroSlide;
use App\Repositories\Contracts\HeroSlideRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class HeroSlideController extends Controller
{
    public function __construct(private HeroSlideRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/admin/hero-slides',
        summary: 'Hero slide listele (Admin)',
        tags: ['Admin - Hero Slides'],
        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: 'Slide listesi')],
    )]
    public function index(Request $request): AnonymousResourceCollection
    {
        $slides = $this->repository->paginate(
            $request->only([]),
            $request->integer('per_page', 15),
        );

        return HeroSlideResource::collection($slides);
    }

    #[OA\Post(
        path: '/api/admin/hero-slides',
        summary: 'Yeni hero slide oluştur',
        tags: ['Admin - Hero Slides'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['title', 'image'],
            properties: [
                new OA\Property(property: 'title', type: 'string'),
                new OA\Property(property: 'subtitle', type: 'string'),
                new OA\Property(property: 'image', type: 'string'),
                new OA\Property(property: 'mobile_image', type: 'string'),
                new OA\Property(property: 'button_text', type: 'string'),
                new OA\Property(property: 'button_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: 'Slide oluşturuldu'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function store(StoreHeroSlideRequest $request, CreateHeroSlideAction $action): JsonResponse
    {
        $dto = HeroSlideData::fromArray($request->validated());
        $slide = $action->execute($dto);

        return response()->json(new HeroSlideResource($slide), 201);
    }

    #[OA\Get(
        path: '/api/admin/hero-slides/{heroSlide}',
        summary: 'Hero slide detayı',
        tags: ['Admin - Hero Slides'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'heroSlide', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Slide detayı')],
    )]
    public function show(HeroSlide $heroSlide): JsonResponse
    {
        return response()->json(new HeroSlideResource($heroSlide));
    }

    #[OA\Put(
        path: '/api/admin/hero-slides/{heroSlide}',
        summary: 'Hero slide güncelle',
        tags: ['Admin - Hero Slides'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'heroSlide', 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: 'subtitle', type: 'string'),
                new OA\Property(property: 'image', type: 'string'),
                new OA\Property(property: 'button_text', type: 'string'),
                new OA\Property(property: 'button_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: 'Slide güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateHeroSlideRequest $request, HeroSlide $heroSlide, UpdateHeroSlideAction $action): JsonResponse
    {
        $dto = HeroSlideData::fromArray(array_merge($heroSlide->toArray(), $request->validated()));
        $heroSlide = $action->execute($heroSlide, $dto);

        return response()->json(new HeroSlideResource($heroSlide));
    }

    #[OA\Delete(
        path: '/api/admin/hero-slides/{heroSlide}',
        summary: 'Hero slide sil',
        tags: ['Admin - Hero Slides'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'heroSlide', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Slide silindi')],
    )]
    public function destroy(HeroSlide $heroSlide, DeleteHeroSlideAction $action): JsonResponse
    {
        $action->execute($heroSlide);

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