<?php
require_once('../../db-connect.php');
$id = $_GET['id'];
$tournament_id = $_GET['tournament_id'];

$db = new DB_CONNECT();
$con = $db->connect();

$result = $con->query("SELECT c.*, u.name AS c_owner_name
FROM contests c
JOIN users u ON c.c_owner_id = u.id
LEFT JOIN joined_contests jc ON jc.contest_id = c.id AND jc.sender_id = '$id'
WHERE (c.c_owner_id = '$id' OR jc.sender_id = '$id') AND c.tournament_id = '$tournament_id'");

$json = $result->fetch_all(MYSQLI_ASSOC);
$success = ($result->num_rows > 0);
$response = array();

if ($result->num_rows > 0) {
  foreach ($json as $row) {
    $currentTimestamp = time();

    // Create a DateTime object from the date string
    $date = DateTime::createFromFormat('d/m/Y h:i A', $row['c_end_date_time']);

    // Convert the DateTime object to a string in a format recognized by strtotime()
    $timestampString = $date->format('Y-m-d H:i:s');

    // Parse the timestamp string using strtotime()
    $timestamp = strtotime($timestampString);

    $isDatePassed = ($currentTimestamp > $timestamp);

    $row['is_date_passed'] = $isDatePassed;
    $row['success'] = $success;
    $response[] = $row;
  }
} else {
  $row['success'] = false;
}

echo json_encode($response);
