<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Controllers\Api;

use App\Core\App;
use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;

class PlayerSearchApiController extends Controller
{
    public function search(Request $request): Response
    {
        $q = trim((string) $request->get('q', ''));
        if (strlen($q) < 2) {
            return $this->json(['results' => []]);
        }

        $db = App::getInstance()->db();
        $like = '%' . $q . '%';

        $results = $db->select(
            "SELECT id, full_name_ar, registration_serial, national_id, player_type, medical_status
             FROM sa_players
             WHERE is_archived = 0
             AND (full_name_ar LIKE ? OR registration_serial LIKE ? OR national_id LIKE ? OR phone LIKE ?)
             ORDER BY full_name_ar
             LIMIT 20",
            [$like, $like, $like, $like]
        );

        return $this->json(['results' => $results]);
    }
}
