<?php 
include '../../config.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$zaman = time();

$sonaktif = time()-24*60*60;

$rand = rand(0,4);

if($rand!=0) die();

function randBot(){
    global $db;
    $query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 1 ORDER BY RAND() LIMIT 1", PDO::FETCH_ASSOC);
    foreach($query as $veri){
        $bot_id = $veri['user_id'];
    }
    return $bot_id;
}


$onesignalArr = array();
$bot_id = randBot();

$query = $db->query("SELECT * FROM kullanicilar WHERE user_songiris >= $sonaktif AND user_bot = 0 AND user_cinsiyet = 'erkek' AND user_id != 663793", PDO::FETCH_ASSOC);

foreach ($query as $row) {
    $begenilerQuery = $db->query("SELECT * FROM begeniler WHERE begenilen_id = '{$row['user_id']}' ORDER BY id DESC")->fetch(PDO::FETCH_ASSOC);

    if (!empty($begenilerQuery)) {
        $zamanfark = $begenilerQuery['zaman'] + 0;
        if (time() < $zamanfark) {
            continue; // Bu satırları atla ve döngüye devam et
        }
    }

    $varMi = $db->query("SELECT COUNT(*) FROM begeniler WHERE begenen_id = {$bot_id} AND begenilen_id = {$row['user_id']}")->fetchColumn();      

    if ($varMi == 0) {
        $insertQuery = $db->prepare("INSERT INTO begeniler SET begenen_id = ?, begenilen_id = ?, zaman = ?");
        $insert = $insertQuery->execute(array($bot_id, $row['user_id'], time()));

        if (preg_match('/^[0-9a-f]{8}-?[0-9a-f]{4}-?4[0-9a-f]{3}-?[89ab][0-9a-f]{3}-?[0-9a-f]{12}$/i', $row['user_onesignal'])) {
            $onesignalArr[] = $row['user_onesignal'];
        }
    }
}

//print_r($onesignalArr);a

/*$rand = rand(0,1);

if($rand!=0) die();*/


$botBilgisi = $db->query("SELECT * FROM kullanicilar WHERE user_id = '{$bot_id}'")->fetch(PDO::FETCH_ASSOC);




$bot_resim = "https://app.heysen.net/".$botBilgisi['user_picture'];

$bot_isim = $botBilgisi['user_fullname'];

$bildirim_icerik = "💌 ".$bot_isim." sizi beğendi.";


      $heading = array(
            "en" => "❤️Yeni Beğeniniz Var!❤️"
        );
    
        $content = array(
            "en" => $bildirim_icerik
        );
    
        $fields = array(
            'app_id' => "ad1b405f-217b-47b5-b59b-770d801cb89b",
            'include_player_ids' => $onesignalArr, 
            'data' => array("foo" => "bar"),
            'contents' => $content,
            'ttl' => 2700,
            'headings' => $heading,
            'priority' => 10,
            'large_icon' => $bot_resim,
            'url' => 'https://'.$_SERVER['SERVER_NAME'].'/profil.php?id='.$botBilgisi['user_id'] 
        );
    

    // 'url' => 'https://'.$_SERVER['SERVER_NAME'].'/mesajlar.php?id='.$bot['user_id']

        $fields = json_encode($fields);
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $response = curl_exec($ch);
        curl_close($ch);

echo $response;   
echo "<br><br>";     


?>
