<?php
    require_once("../../php/db_utils.php");
    
    try {
        // Connect to the database
        $database = databaseConnect();
        
        // Incoming data
        $currentTag = $_POST["tag"];
        $username = $_POST["username"];
        
        // Declare query
        $query = "SELECT tag, description, username FROM tags WHERE NOT username='" . $username . "' ORDER BY tag ASC;";
        
        // Run query
        $recordSet = databaseQueryWithReturn($database, $query);
        
        // Get similar tags
        $tags = Array();
        
        $index = 0;
        
        while($record = $recordSet->fetch_assoc()) {
            $percentage;
            
            similar_text($currentTag, $record["tag"], $percentage);
            
            if($percentage > 75) {
                $tags[$index]["tag"] = $record["tag"];
                $tags[$index]["description"] = $record["description"];
                $tags[$index]["username"] = $record["username"];
                
                $index++;
            }
        }
        
        mysqli_free_result($recordSet);
        
        echo(json_encode($tags));
        
        // Close the connection to the database
        databaseClose($database);
    }
    catch(Exception $e) {
        $errorMsg = $e->getMessage();
        print_r($errorMsg);
    }
?>