<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\Comment\DeleteCommentAction;
use App\Actions\Comment\UpdateCommentAction;
use App\Http\Controllers\Controller;
use App\Http\Requests\Comment\UpdateCommentRequest;
use App\Http\Resources\CommentResource;
use App\Models\Comment;
use App\Repositories\Contracts\CommentRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class CommentController extends Controller
{
    public function __construct(private CommentRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/admin/comments',
        summary: 'Yorumları listele (Admin)',
        tags: ['Admin - Comments'],
        security: [['sanctum' => []]],
        parameters: [
            new OA\Parameter(name: 'is_approved', in: 'query', required: false, schema: new OA\Schema(type: 'boolean')),
            new OA\Parameter(name: 'commentable_type', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
            new OA\Parameter(name: 'search', 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: 15)),
        ],
        responses: [new OA\Response(response: 200, description: 'Yorum listesi')],
    )]
    public function index(Request $request): AnonymousResourceCollection
    {
        $comments = $this->repository->paginate(
            $request->only(['is_approved', 'commentable_type', 'search']),
            $request->integer('per_page', 15),
        );

        return CommentResource::collection($comments);
    }

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

    #[OA\Put(
        path: '/api/admin/comments/{comment}',
        summary: 'Yorum güncelle (onayla/reddet)',
        tags: ['Admin - Comments'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'comment', 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: 'is_approved', type: 'boolean'),
            ],
        )),
        responses: [
            new OA\Response(response: 200, description: 'Yorum güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateCommentRequest $request, Comment $comment, UpdateCommentAction $action): JsonResponse
    {
        $comment = $action->execute($comment, $request->validated());

        return response()->json(new CommentResource($comment));
    }

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

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