<?php
chdir(__DIR__);
require "../configs/configs.php";
require "../libs/functions.php";
$limit = 100;
$stmt  = $pdo->prepare("SELECT `id`,`user_id`,`text` FROM `send_all` ORDER BY `id` ASC LIMIT :limit");
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();

if($stmt->rowCount() == 0) {
    die("هیچ پیامی برای ارسال وجود ندارد.");
}

$deleteStmt = $pdo->prepare("DELETE FROM `send_all` WHERE `id` = :id");
$success    = 0;
$fail       = 0;

while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
    $user_id = intval($row['user_id']);
    $text    = $row['text'];
    $id      = $row['id'];
    $resp    = null;

if($user_id > 0) {
        $chatInfo = getChat($user_id);
        $chatType = $chatInfo->result->type ?? null;

if($chatType === 'private') {
            $resp = sendMessage($user_id, $text);
}

}


if($resp && isset($resp->ok) && $resp->ok) {
        $success++;
} else {
        $fail++;
}

    $deleteStmt->execute(['id' => $id]);
}


die;
