<?php
declare(strict_types=1);

namespace App\Modules\Members\Services;

use App\Core\App;

final class MemberSearchService
{
    public static function search(string $query, int $limit = 25): array
    {
        $db = App::getInstance()->db();
        $query = trim($query);

        if ($query === '') {
            return [];
        }

        $like = '%' . $query . '%';

        return $db->select(
            "SELECT m.id, m.full_name_ar, m.full_name_en, m.national_id, 
                    m.membership_number, m.phone_mobile, m.status, m.form_number,
                    b.name_ar as branch_name
             FROM members m
             LEFT JOIN branches b ON b.id = m.branch_id
             WHERE m.is_archived = 0
             AND (
                 m.full_name_ar LIKE ? 
                 OR m.national_id LIKE ? 
                 OR m.membership_number LIKE ? 
                 OR m.phone_mobile LIKE ?
                 OR m.form_number LIKE ?
                 OR m.full_name_en LIKE ?
             )
             ORDER BY m.full_name_ar
             LIMIT ?",
            [$like, $like, $like, $like, $like, $like, $limit]
        );
    }
}