<?php
require "/home/hpr/php/include.php";

if ( $_SERVER['REQUEST_METHOD'] !== 'GET' ) {
    naughty("438a220a58dc34c200a8669547afc66b"); 
}

$num_get_args=0;
foreach($_GET as $k => $v) { 
    ++$num_get_args; 
}
if ( $num_get_args !== 2 ){
    $result = mysqli_query($connection, "SET time_zone = '+0:00';");
    if (!isset($result)) {
        naughty("cca6408ae1febc3f07974177b2d04375");
    }

    header("Content-type: text/csv");
    print "timestamp_epoc,ep_num,ep_date,key,status,email\n";
    # 1649790226_3710_2022-10-21_b291590ce4ba23b519935bde53d0a5936255cd12e96b4
    $query = "SELECT UNIX_TIMESTAMP(timestamp) as timestamp_epoc, `ep_num`, `ep_date`, `key`, `status`, `email` FROM `reservations` WHERE `ep_num` > 0 ORDER BY `ep_num` ASC";
    if($result = mysqli_query($connection, $query)) {
        while($row = mysqli_fetch_array($result)) {
            //handle text color
            print "{$row['timestamp_epoc']},{$row['ep_num']},{$row['ep_date']},{$row['key']},{$row['status']},{$row['email']}\n";
        }
    }
    else {
        naughty("2dcabda45255713ea9bf04523e804016");
    }
    mysqli_close($connection);
    exit;
}

if ( empty($_GET["ep_num"]) or empty($_GET["status"]) ) {
    naughty("32bedea129648f27701f2f2f3ff4b7b5");
}

if ( !in_array($_GET["status"], array('METADATA_PROCESSED','SHOW_POSTED','MEDIA_TRANSCODED','UPLOADED_TO_IA','UPLOADED_TO_RSYNC_NET','REMOVE_RESERVATION'), true ) ) {
    naughty("88882bc97094e7aabf2a258756f917f5 ");
}
else {
    $status = $_GET["status"];
}

if ( !empty($_GET["ep_num"]) and isset( $_GET["ep_num"] ) ) {
    $ep_num = intval( $_GET["ep_num"] );
}
else {
    naughty("ecb0ebc5b38b4c09226ccbfce05978cb");
}

// // SELECT MAX(ep_num) FROM `reservations` → 3627
// // SELECT MIN(ep_num) FROM `reservations` WHERE ep_num > 0 → 3582
// 
$result = mysqli_query($connection, 'SELECT MAX(ep_num) FROM `reservations`;');
if (!isset($result)) {
    naughty("360e6381ffca736b6f12056abd7dcc7d");
}
$max_eps_array = mysqli_fetch_row( $result );
$max_eps = $max_eps_array[0];
mysqli_free_result($result);

$result = mysqli_query($connection, 'SELECT MIN(ep_num) FROM `reservations` WHERE ep_num > 0;');
if (!isset($result)) {
    naughty("6971b6d378292e8fc9583de6082eddf4");
}
$min_eps_array = mysqli_fetch_row( $result );
$min_eps = $min_eps_array[0];
mysqli_free_result($result);

if ( empty( $ep_num )  ) {
    naughty("6cf46a9b21e667c61b2c5bf87a7104f9 $ep_num");
}

if ( $ep_num < $min_eps ) {
    naughty("bbcef7cd8a263dc57e85195935ad600a $ep_num < $min_eps");
}

if ( $ep_num > $max_eps ) {
    naughty("a75115f3fa706ceee4b61971cd22b8f9 $ep_num > $max_eps");
}

// SHOW_SUBMITTED → METADATA_PROCESSED → SHOW_POSTED → MEDIA_TRANSCODED → UPLOADED_TO_IA → UPLOADED_TO_RSYNC_NET
switch ($status) {
  case "METADATA_PROCESSED":
    $current_status="SHOW_SUBMITTED";
    break;
  case "SHOW_POSTED":
    $current_status="METADATA_PROCESSED";
    break;
  case "MEDIA_TRANSCODED":
    $current_status="SHOW_POSTED";
    break;
  case "UPLOADED_TO_IA":
    $current_status="MEDIA_TRANSCODED";
    break;
  case "UPLOADED_TO_RSYNC_NET":
    $current_status="UPLOADED_TO_IA";
    break;
  case "REMOVE_RESERVATION":
    $query = "DELETE FROM reservations WHERE `ep_num` = '$ep_num' ";
    $result = @mysqli_query($connection, $query);
    if (mysqli_errno( $connection)) {
    $error = "MySQL error ".mysqli_errno( $connection).": ".mysqli_error($connection)."\n";
      problem("Could not update the show reservation to $status in the db");
    }
    mysqli_free_result($result);
    mysqli_close($connection);
    logextra( "$query");
    die;
}

$result = mysqli_query($connection, "SELECT ep_num FROM reservations WHERE `ep_num` = '$ep_num' AND status='$current_status';");
if (!isset($result)) {
    naughty("7f2d7228ca355be6dd2a24769595b18f");
}
$db_ep_num_array = mysqli_fetch_row( $result );
$db_ep_num = $db_ep_num_array[0];
mysqli_free_result($result);
if ( $db_ep_num != $ep_num ){
    problem("da59731c6ae6d1ce0fa2fa9fc4e1e726 $db_ep_num != $ep_num");
}

$query = "UPDATE reservations SET `status` = '$status' WHERE `ep_num` = '$ep_num' AND status='$current_status';";
$result = mysqli_query($connection,  $query );
if(!isset($result)) {
	problem("8a76bb408877b1d33bf31ea96b6fc02f");
}
if (mysqli_errno( $connection )) {
  $error = "MySQL error ".mysqli_errno( $connection).": ".mysqli_error($connection)."\n";
  problem("Could not update the show reservation to $status in the db");
}
mysqli_close($connection);

logextra( "UPDATE reservations SET `status` = '$status' WHERE ep_num='$ep_num' AND status='$current_status';");
?>