<?php
// File: /v1/admin/error/error_search_by_phone.php
require_once __DIR__ . '/../../connect.php';

try {
    $phone = filterRequest("phone");
    if ($phone === false || $phone === null || trim($phone) === "") {
        jsonError("Phone is required");
        exit;
    }

    // في حال مخزّن الهاتف مشفّر، طبق نفس دالتك للتشفير هنا:
    // $enc_phone = $encryptionHelper->encryptData(trim($phone));
    // ثم بدّل الحقل في WHERE إلى phone = :ph
    $sql = "SELECT `id`, `error`, `userId`, `userType`, `phone`, `created_at`, `device`, `details`, `status`
            FROM `error`
            WHERE `phone` = :ph OR `phone` LIKE :phLike
            ORDER BY `created_at` DESC
            LIMIT 20";

    $stmt = $con->prepare($sql);
    $stmt->execute([
        ":ph"     => trim($phone),
        ":phLike" => '%' . trim($phone) . '%', // يسمح بجزء من الرقم إن أردت
    ]);

    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    jsonSuccess($rows);
} catch (Exception $e) {
    error_log("error_search_by_phone.php: " . $e->getMessage());
    jsonError($message = "Failed to search errors by phone");
}