<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Http\Resources\StoryResource;
use App\Models\Story;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class StoryController extends Controller
{
    #[OA\Get(
        path: '/api/admin/stories',
        summary: 'Tüm hikayeleri listele (Admin)',
        tags: ['Admin - Stories'],
        security: [['sanctum' => []]],
        responses: [new OA\Response(response: 200, description: 'Hikaye listesi')],
    )]
    public function index(): AnonymousResourceCollection
    {
        return StoryResource::collection(
            Story::query()->orderBy('order_index')->get()
        );
    }

    #[OA\Post(
        path: '/api/admin/stories',
        summary: 'Yeni hikaye oluştur',
        tags: ['Admin - Stories'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['title', 'content'],
            properties: [
                new OA\Property(property: 'title', type: 'string'),
                new OA\Property(property: 'badge', type: 'string', nullable: true),
                new OA\Property(property: 'content', type: 'string'),
                new OA\Property(property: 'image', type: 'string', nullable: true),
                new OA\Property(property: 'cta_text', type: 'string', nullable: true),
                new OA\Property(property: 'cta_url', type: 'string', nullable: true),
                new OA\Property(property: 'order_index', type: 'integer'),
                new OA\Property(property: 'is_active', type: 'boolean'),
            ],
        )),
        responses: [
            new OA\Response(response: 201, description: 'Hikaye oluşturuldu'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'title' => ['required', 'string', 'max:255'],
            'badge' => ['nullable', 'string', 'max:100'],
            'content' => ['required', 'string'],
            'image' => ['nullable', 'string', 'max:255'],
            'cta_text' => ['nullable', 'string', 'max:100'],
            'cta_url' => ['nullable', 'string', 'max:255'],
            'order_index' => ['sometimes', 'integer', 'min:0'],
            'is_active' => ['sometimes', 'boolean'],
        ]);

        $story = Story::create($validated);

        return response()->json(new StoryResource($story), 201);
    }

    #[OA\Get(
        path: '/api/admin/stories/{story}',
        summary: 'Hikaye detayı',
        tags: ['Admin - Stories'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'story', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Hikaye detayı')],
    )]
    public function show(Story $story): JsonResponse
    {
        return response()->json(new StoryResource($story));
    }

    #[OA\Put(
        path: '/api/admin/stories/{story}',
        summary: 'Hikaye güncelle',
        tags: ['Admin - Stories'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'story', 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: 'badge', type: 'string', nullable: true),
                new OA\Property(property: 'content', type: 'string'),
                new OA\Property(property: 'image', type: 'string', nullable: true),
                new OA\Property(property: 'cta_text', type: 'string', nullable: true),
                new OA\Property(property: 'cta_url', type: 'string', nullable: true),
                new OA\Property(property: 'order_index', type: 'integer'),
                new OA\Property(property: 'is_active', type: 'boolean'),
            ],
        )),
        responses: [
            new OA\Response(response: 200, description: 'Hikaye güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(Request $request, Story $story): JsonResponse
    {
        $validated = $request->validate([
            'title' => ['sometimes', 'string', 'max:255'],
            'badge' => ['nullable', 'string', 'max:100'],
            'content' => ['sometimes', 'string'],
            'image' => ['nullable', 'string', 'max:255'],
            'cta_text' => ['nullable', 'string', 'max:100'],
            'cta_url' => ['nullable', 'string', 'max:255'],
            'order_index' => ['sometimes', 'integer', 'min:0'],
            'is_active' => ['sometimes', 'boolean'],
        ]);

        $story->update($validated);

        return response()->json(new StoryResource($story->fresh()));
    }

    #[OA\Delete(
        path: '/api/admin/stories/{story}',
        summary: 'Hikaye sil',
        tags: ['Admin - Stories'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'story', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Hikaye silindi')],
    )]
    public function destroy(Story $story): JsonResponse
    {
        $story->delete();

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