<?php

namespace App\Http\Controllers;

use App\Models\Artist;
use Illuminate\Http\Request;
use OpenApi\Annotations as OA;

/**
 * @OA\Tag(
 *     name="Artist",
 *     description="藝人資料搜尋"
 * )
 */
class ArtistController extends Controller
{
    /**
     * @OA\Get(
     *     path="/api/artists/search",
     *     tags={"Artist"},
     *     summary="搜尋藝人名稱",
     *     @OA\Parameter(
     *         name="search",
     *         in="query",
     *         description="關鍵字搜尋藝人名稱",
     *         required=false,
     *         @OA\Schema(type="string")
     *     ),
     *     @OA\Parameter(
     *         name="selected",
     *         in="query",
     *         description="已選擇藝人 ID 陣列",
     *         required=false,
     *         @OA\Schema(type="array", @OA\Items(type="integer"))
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="搜尋結果"
     *     )
     * )
     */
    public function search(Request $request)
    {
        $query = Artist::query();

        if ($request->filled('selected')) {
            // 取得已選擇的項目（初始化時用）
            return $query->whereIn('id', (array) $request->input('selected'))
                        ->get(['id', 'name']);
        }

        if ($request->filled('search')) {
            // 關鍵字搜尋（選單輸入文字時用）
            $search = $request->input('search');
            $query->where('name', 'like', "%{$search}%");
        }

        return $query->limit(10)->get(['id', 'name']);
    }

}