<?php
	include('../extension/connect.php');
	require "../extension/config.php";
	
	$ip = $db->get_client_ip();
    if($ip == 'UNKNOWN')
    {
    	$con =  mysqli_connect($db_host,$db_user,$db_pass,$db_name);
    	if(mysqli_connect_error()){
    		echo 'connect to database failed';
    	} else {
    	    // Decrement user_duration for all users regardless of is_freeze status
    	    $query = mysqli_query($con,"UPDATE users SET user_duration=user_duration-15 WHERE user_duration>0 and device_connected=1");
    	    
    	    // Set user_duration to 0 if it is less than 1
    	    $query2 = mysqli_query($con,"UPDATE users SET user_duration=0 WHERE user_duration<1 and device_connected=1");
    	    
    	    // Update superadmin credits
    	    $query3 = mysqli_query($con,"UPDATE users SET user_credits='99999999' WHERE user_id='1' and user_rank='superadmin'");
    	    
           // Uncomment the delete queries if you want to remove expired accounts
    	   // Remove expired users if desired
    	  // $query4 = mysqli_query($con,"DELETE FROM users WHERE user_duration<1 and device_connected=1 and (user_rank='normal' OR user_rank='export')");
    	   // Optionally remove related radcheck records
    	   // $query5 = mysqli_query($con,"DELETE FROM radcheck WHERE username IN (SELECT user_name FROM users WHERE user_duration<1)");

    	    $qry_ = mysqli_query($con,"SELECT user_name FROM users WHERE user_duration<1");
    	    while($row_ = mysqli_fetch_array($qry_)) {
                $uname = $row_['user_name'];
                
            	// Optionally remove radcheck entries related to expired users
                // $query6 = mysqli_query($con,"DELETE FROM radcheck WHERE username = '$uname'");
            }
        }
    }else{
	    echo '<script> alert("Opss! Invalid request detected! Your IP Address: '.$ip.'"); window.location.href="https://t.me/techydev"; </script>';
    }
?>
