<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\Faq\CreateFaqAction;
use App\Actions\Faq\DeleteFaqAction;
use App\Actions\Faq\UpdateFaqAction;
use App\DTOs\FaqData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Faq\StoreFaqRequest;
use App\Http\Requests\Faq\UpdateFaqRequest;
use App\Http\Resources\FaqResource;
use App\Models\Faq;
use App\Repositories\Contracts\FaqRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class FaqController extends Controller
{
    public function __construct(private FaqRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/admin/faqs',
        summary: 'SSS listele (Admin)',
        tags: ['Admin - FAQs'],
        security: [['sanctum' => []]],
        parameters: [
            new OA\Parameter(name: 'category', 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: 'SSS listesi')],
    )]
    public function index(Request $request): AnonymousResourceCollection
    {
        $faqs = $this->repository->paginate(
            $request->only(['category']),
            $request->integer('per_page', 50),
        );

        return FaqResource::collection($faqs);
    }

    #[OA\Post(
        path: '/api/admin/faqs',
        summary: 'Yeni SSS oluştur',
        tags: ['Admin - FAQs'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            required: ['question', 'answer', 'category'],
            properties: [
                new OA\Property(property: 'question', type: 'string'),
                new OA\Property(property: 'answer', type: 'string'),
                new OA\Property(property: 'category', 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: 'SSS oluşturuldu'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function store(StoreFaqRequest $request, CreateFaqAction $action): JsonResponse
    {
        $dto = FaqData::fromArray($request->validated());
        $faq = $action->execute($dto);

        return response()->json(new FaqResource($faq), 201);
    }

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

    #[OA\Put(
        path: '/api/admin/faqs/{faq}',
        summary: 'SSS güncelle',
        tags: ['Admin - FAQs'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'faq', 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: 'question', type: 'string'),
                new OA\Property(property: 'answer', type: 'string'),
                new OA\Property(property: 'category', 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: 'SSS güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateFaqRequest $request, Faq $faq, UpdateFaqAction $action): JsonResponse
    {
        $dto = FaqData::fromArray(array_merge($faq->toArray(), $request->validated()));
        $faq = $action->execute($faq, $dto);

        return response()->json(new FaqResource($faq));
    }

    #[OA\Delete(
        path: '/api/admin/faqs/{faq}',
        summary: 'SSS sil',
        tags: ['Admin - FAQs'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'faq', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'SSS silindi')],
    )]
    public function destroy(Faq $faq, DeleteFaqAction $action): JsonResponse
    {
        $action->execute($faq);

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