<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Api\Admin\Concerns\AuthorizesOwnedResources;
use App\Http\Controllers\Controller;
use App\Models\QuestionBank;
use App\Models\QuestionCategory;
use App\Models\QuestionTag;
use App\Support\ApiResponse;
use hg\apidoc\annotation as Apidoc;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

#[Apidoc\Group('后台')]
#[Apidoc\Title('分类与标签')]
#[Apidoc\RouteMiddleware(['jwt.auth'])]
final class TaxonomyController extends Controller
{
    use AuthorizesOwnedResources;

    #[Apidoc\Title('分类列表')]
    #[Apidoc\Url('/admin/banks/{bank}/categories')]
    #[Apidoc\Method('GET')]
    #[Apidoc\RouteMiddleware(['permission:questions'])]
    public function categories(Request $request, mixed $bank): JsonResponse
    {
        $bank = $this->resolveBank($bank);
        $this->authorizeBankOwner($request, $bank);

        return ApiResponse::success($bank->categories()->orderBy('sort')->get());
    }

    #[Apidoc\Title('创建分类')]
    #[Apidoc\Url('/admin/banks/{bank}/categories')]
    #[Apidoc\Method('POST')]
    #[Apidoc\RouteMiddleware(['permission:banks.update'])]
    public function createCategory(Request $request, mixed $bank): JsonResponse
    {
        $bank = $this->resolveBank($bank);
        $this->authorizeBankOwner($request, $bank);

        $data = $request->validate([
            'name' => ['required', 'string', 'max:100'],
            'parent_id' => ['nullable', 'exists:question_categories,id'],
            'sort' => ['nullable', 'integer', 'min:0'],
        ]);

        $category = QuestionCategory::create($data + ['question_bank_id' => $bank->id]);

        return ApiResponse::success($category, '分类已创建');
    }

    #[Apidoc\Title('标签列表')]
    #[Apidoc\Url('/admin/banks/{bank}/tags')]
    #[Apidoc\Method('GET')]
    #[Apidoc\RouteMiddleware(['permission:questions'])]
    public function tags(Request $request, mixed $bank): JsonResponse
    {
        $bank = $this->resolveBank($bank);
        $this->authorizeBankOwner($request, $bank);

        return ApiResponse::success($bank->tags()->orderBy('name')->get());
    }

    #[Apidoc\Title('创建标签')]
    #[Apidoc\Url('/admin/banks/{bank}/tags')]
    #[Apidoc\Method('POST')]
    #[Apidoc\RouteMiddleware(['permission:banks.update'])]
    public function createTag(Request $request, mixed $bank): JsonResponse
    {
        $bank = $this->resolveBank($bank);
        $this->authorizeBankOwner($request, $bank);

        $data = $request->validate(['name' => ['required', 'string', 'max:50']]);

        $tag = QuestionTag::firstOrCreate([
            'question_bank_id' => $bank->id,
            'name' => $data['name'],
        ]);

        return ApiResponse::success($tag, '标签已创建');
    }

    private function resolveBank(mixed $bank): QuestionBank
    {
        if ($bank instanceof QuestionBank && $bank->exists) {
            return $bank;
        }

        return QuestionBank::query()->findOrFail((int) $bank);
    }
}
