<?php

include_once __DIR__ . '/../secrets.php';
include '../db_connect.php';

$sender = $_POST['sender_id'];
$receiver = $_POST['receiver_id'];
$time = $_POST['time'];
$key  = $_POST['key'];

$security_key = decrypData($key);

if ($security_key === false) {
    echo 'Invalid encryption data';
    exit;
}

if ($security_key == SECURITY_KEY_PLAIN) {
    

$con = dbConnect();

$sql = "SELECT * FROM friends_request
        WHERE sender_id='$sender'
        AND receiver_id='$receiver'";

$result = mysqli_query($con, $sql);

if(mysqli_num_rows($result) > 0){
    echo "already_sent";
    exit;
}

$sql = "INSERT INTO friends_request(sender_id, receiver_id, status, time)
        VALUES('$sender','$receiver','cancelled', '$time')";

if(mysqli_query($con, $sql)){
    echo "success";
} else {
    echo "error";
}

}

?>
