<?php

include('database_connection.php');
$data = "empty";

if(isset($_POST['data'])){
$data = $_POST['data'];
}





if(isset($_GET['data'])){
$data = $_GET['data'];

   // WHERE payment_id = '".$_GET["data"]."'
//    ";
}



// Receive POST data
$message = $_POST['message'];
    $message = file_get_contents('php://input');

// Decode JSON data if applicable
$data = json_decode($message, true);


// Example: Retrieve alert message and symbol
$alertMessage = $data['message'];
$symbol = $data['symbol'];
$direction = "";

// Sample message
// $message = "The market is Downtrending and showing bearish signals.";

// Initialize $direction variable
$direction = "";

// Check if $message contains "Downtrending"
if (strpos($message, "Downtrending") !== false) {
    $direction = "Downtrending";
}

// Check if $message contains "Uptrending"
if (strpos($message, "Uptrending") !== false) {
    $direction = "Uptrending";
}

// Output the result
echo "Direction: $direction";


date_default_timezone_set('Europe/London'); 
$dateTime = date("Y-m-d 
H:i:s");


// Sample message
// $message = "BTC/USD is Uptrending. Alignment: Positive.";

// Initialize $coin variable
$coin = "";

// Split the message based on the word "Alignment"
$parts = explode("Alignment", $message);

// Use the first part as the value for $coin
if (count($parts) > 0) {
    $coin = trim($parts[0]);
}

// Output the result
// echo "Coin: $coin";


// Sample message
// $message = "BTC/USD is Uptrending. RSI = 70. Alignment: Positive.";

// Initialize $rsi variable
$rsi = "";

// Find the position of the phrase "RSI = " in the message
$position = strpos($message, "RSI = ");

// Check if "RSI = " is found in the message
if ($position !== false) {
    // Extract the substring starting from the position of "RSI = "
    $rsi = intval(trim(substr($message, $position + strlen("RSI = "))));
}

// Output the result
// echo "RSI: $rsi";

//  $query = "INSERT into tradingview (date, alignment, coin, rsi) VALUES ('$dateTime', '$direction', '$coin', '$rsi') ";
 $query = "INSERT into tradingview (data, date, alignment, coin, rsi) VALUES ('$message', '$dateTime', '$direction', '$coin', '$rsi') ";

 $statement = $connect->prepare($query);
 $statement->execute();
print $_GET['data'];
print "POST = ";
print $message;
header('HTTP/1.1 200 OK');
echo 'Webhook received successfully!';
?>
