Reply Depend on current and previous Status

I have this code tend to achieve this

If the user has previously asked about Nrat and then says “Thank you”: The reply should respond with a specific Nrat-related response.
If the user just says “Thank you” without previously asking about Nrat: The reply should respond with a general “Thank you” response.

but reply always triggers to General thanks even user ask previous about Nrat

<?php
// include_once 'conection.php';

// session_start();

    if (isset($_SESSION['conversation_state'])) {
        $sessionUser = $_SESSION['conversation_state'];

        // Prepare and execute the query to fetch the conversation state
        $stmt = $mysqli->prepare("SELECT `last_intent`, `context` FROM `conversation_states` WHERE `Session_User` = ? ORDER BY `timestamp` DESC LIMIT 2");
        if ($stmt === false) {
            die('Prepare failed: ' . $mysqli->error);
        }
        
        $stmt->bind_param("s", $sessionUser);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result === false) {
            die('Execute failed: ' . $stmt->error);
        }

        if ($result->num_rows > 0) {
            $intents = [];
            while ($row = $result->fetch_assoc()) {
                $intents[] = $row['last_intent'];
            }
    

            $currentIntent = $intents[0];
        } 
            
    }else {
        
    }
    

    
    $checkcurrentStatus=false;

    
    function handleDialogyManagement( ) {
        global $lastIntent,$currentIntent,$checkcurrentStatus;
        $checkcurrentStatus=false;
        if($checkcurrentStatus){
            if($lastIntent === 'Murat_Identity' || $lastIntent === 'Murat_Office_Location'){
                $checkcurrentStatus=true;
                }else{
                    $checkcurrentStatus=false;
            }
         
        }
        
         // Check if last intent was 'Murat_Identity' or 'Murat_Office_Location' and current input is 'Thank_You'
         if (($lastIntent === 'Murat_Identity' || $lastIntent === 'Murat_Office_Location') && $currentIntent === 'Thank_You') {
           
         
            // Generate a random index to select one of the predefined responses
            $randomIndex = rand(0, 2); // Adjust the range based on the number of responses
    
            // Predefined responses based on your requirements
            $responses = [
                "I'm happy to help about Nrat. If you have another question, feel free to ask.",
                "I'm happy to further assist you about Nrat. You can like my response if it's helpful for my training.",
                "I'm grateful that my answer about Nrat has helped you. If you have another question, feel free to ask."
        
                // Add more responses here
            ];
    
            
            // Assign the selected response to the answer field in the response array
            $response['answer'] = $responses[$randomIndex];

            return $response;
        }

        // Check if last intent was 'Murat_Identity' or 'Murat_Office_Location' and current input is 'Thank_You'
        if ( $currentIntent === 'Thank_You' && $checkcurrentStatus===false) {
            // Generate a random index to select one of the predefined responses
            $randomIndex = rand(0, 2); // Adjust the range based on the number of responses
    
            // Predefined responses based on your requirements
            $responses = [
              "You're welcome! If you need further assistance, feel free to ask.",
"My pleasure. Let me know if there's anything else I can help with.",
"No problem! If you have any more questions, just let me know.",
"Anytime! I'm here whenever you need help.",
"Happy to help. Don't hesitate to reach out if you need anything else.",

            ];
    
            
            // Assign the selected response to the answer field in the response array
            $response['answer'] = $responses[$randomIndex];

            return $response;
        }

    
            
    
    }
    
    

?>

1-If the user has previously asked about Nrat and then says “Thank you”: The reply should respond with a specific Nrat-related response.

2-If the user just says “Thank you” without previously asking about Nrat: The reply should respond with a general “Thank you” response.