<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\Category\CreateCategoryAction;
use App\Actions\Category\DeleteCategoryAction;
use App\Actions\Category\UpdateCategoryAction;
use App\DTOs\CategoryData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Category\StoreCategoryRequest;
use App\Http\Requests\Category\UpdateCategoryRequest;
use App\Http\Resources\CategoryResource;
use App\Models\Category;
use App\Repositories\Contracts\CategoryRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class CategoryController extends Controller
{
    public function __construct(private CategoryRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/admin/categories',
        summary: 'Kategorileri listele (Admin)',
        tags: ['Admin - Categories'],
        security: [['sanctum' => []]],
        parameters: [
            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: 'Kategori listesi')],
    )]
    public function index(Request $request): AnonymousResourceCollection
    {
        $categories = $this->repository->paginate(
            filters: $request->only('search'),
            perPage: $request->integer('per_page', 15),
        );

        return CategoryResource::collection($categories);
    }

    #[OA\Post(
        path: '/api/admin/categories',
        summary: 'Yeni kategori oluştur',
        tags: ['Admin - Categories'],
        security: [['sanctum' => []]],
        requestBody: new OA\RequestBody(
            required: true,
            content: new OA\JsonContent(
                required: ['name'],
                properties: [
                    new OA\Property(property: 'name', type: 'string'),
                    new OA\Property(property: 'slug', type: 'string'),
                    new OA\Property(property: 'description', type: 'string'),
                    new OA\Property(property: 'image', type: 'string'),
                    new OA\Property(property: 'icon', type: 'string'),
                    new OA\Property(property: 'is_active', type: 'boolean'),
                    new OA\Property(property: 'sort_order', type: 'integer'),
                    new OA\Property(property: 'meta_title', type: 'string'),
                    new OA\Property(property: 'meta_description', type: 'string'),
                ],
            ),
        ),
        responses: [
            new OA\Response(response: 201, description: 'Kategori oluşturuldu'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function store(StoreCategoryRequest $request, CreateCategoryAction $action): JsonResponse
    {
        $dto = CategoryData::fromArray($request->validated());
        $category = $action->execute($dto);

        return (new CategoryResource($category))
            ->response()
            ->setStatusCode(201);
    }

    #[OA\Get(
        path: '/api/admin/categories/{category}',
        summary: 'Kategori detayı (Admin)',
        tags: ['Admin - Categories'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'category', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [
            new OA\Response(response: 200, description: 'Kategori detayı'),
            new OA\Response(response: 404, description: 'Bulunamadı'),
        ],
    )]
    public function show(Category $category): CategoryResource
    {
        return new CategoryResource($category);
    }

    #[OA\Put(
        path: '/api/admin/categories/{category}',
        summary: 'Kategori güncelle',
        tags: ['Admin - Categories'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'category', 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: 'name', type: 'string'),
                new OA\Property(property: 'slug', type: 'string'),
                new OA\Property(property: 'description', type: 'string'),
                new OA\Property(property: 'image', type: 'string'),
                new OA\Property(property: 'icon', type: 'string'),
                new OA\Property(property: 'is_active', type: 'boolean'),
                new OA\Property(property: 'sort_order', type: 'integer'),
                new OA\Property(property: 'meta_title', type: 'string'),
                new OA\Property(property: 'meta_description', type: 'string'),
            ],
        )),
        responses: [
            new OA\Response(response: 200, description: 'Kategori güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateCategoryRequest $request, Category $category, UpdateCategoryAction $action): CategoryResource
    {
        $dto = CategoryData::fromArray($request->validated());
        $category = $action->execute($category, $dto);

        return new CategoryResource($category);
    }

    #[OA\Delete(
        path: '/api/admin/categories/{category}',
        summary: 'Kategori sil',
        tags: ['Admin - Categories'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'category', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [
            new OA\Response(response: 200, description: 'Kategori silindi'),
            new OA\Response(response: 404, description: 'Bulunamadı'),
        ],
    )]
    public function destroy(Category $category, DeleteCategoryAction $action): JsonResponse
    {
        $action->execute($category);

        return response()->json(['message' => 'Kategori başarıyla silindi.']);
    }
}
