<?php

class AccessToken
{
    // Database connection details
    private static $host = 'localhost';
    private static $user = 'notafwjb_hamid';
    private static $password = 'Helpdesk#@24';
    private static $db = 'notafwjb_ticketdesk24';

    // Property to store the connection
    private static $mysqli = null;

    // Static method to get the database connection
    public static function getConnection()
    {
        if (self::$mysqli === null) {
            self::$mysqli = new mysqli(self::$host, self::$user, self::$password, self::$db);

            if (self::$mysqli->connect_error) {
                die("Connection failed: " . self::$mysqli->connect_error);
            }
        }
        return self::$mysqli;
    }

    // Function to get the access token by user ID
    public static function getAccessTokenByUserId($userId)
    {
        $mysqli = self::getConnection();  // Get DB connection

        $userId = (int)$userId;  // Ensure the user ID is an integer to prevent SQL injection
        $query = "SELECT acsess_token, client_endpoint,respo_id  FROM bitrix24_token WHERE bitrix24_user_id = $userId LIMIT 1";
        $result = $mysqli->query($query);

        if ($result && $row = $result->fetch_assoc()) {
            return [
                'access_token' => $row['acsess_token'],
                'client_endpoint'  => $row['client_endpoint'],
                'repo_id'  => $row['respo_id']
            ];
        }

        return null;  // Return null if no token is found
    }
}


?>
