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

if ( $_SERVER['REQUEST_METHOD'] !== 'GET' ) {
    naughty("438a220a58dc34c200a8669547afc66b REQUEST_METHOD not supported"); 
}
logextra( "GET Method used." );

$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 DB Error");
    }
    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 Query error");
    }
    logextra( "Got status from DB" );
    mysqli_close($connection);
    exit;
}

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

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

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

// // 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 Cant get max from db");
}
$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 Cant get min from db");
}
$min_eps_array = mysqli_fetch_row( $result );
$min_eps = $min_eps_array[0];
mysqli_free_result($result);
logextra( "Got smallest ep_num from db" );

if ( empty( $ep_num )  ) {
    naughty("6cf46a9b21e667c61b2c5bf87a7104f9 Empty ep_num");
}
logextra( "ep_num is not empty" );

if ( $ep_num < $min_eps ) {
    naughty("bbcef7cd8a263dc57e85195935ad600a Invalid $ep_num < $min_eps");
}
logextra( "ep_num is not less than the minimum ep" );

if ( $ep_num > $max_eps ) {
    naughty("a75115f3fa706ceee4b61971cd22b8f9 Invalid $ep_num > $max_eps");
}
logextra( "ep_num is not greater than max ep" );

// 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");
    }
    logextra( "Was able to remove the reservation for $ep_num" );
    mysqli_free_result($result);
    mysqli_close($connection);
    logextra( "$query");
    die;
}
logextra( "Set current status to $current_status" );

$result = mysqli_query($connection, "SELECT ep_num FROM reservations WHERE `ep_num` = '$ep_num' AND status='$current_status';");
if (!isset($result)) {
    naughty("7f2d7228ca355be6dd2a24769595b18f Cant get reservation");
}
logextra( "Retrieved the reservation for $ep_num with status of $current_status" );

$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 Error $db_ep_num != $ep_num");
}
logextra( "The ep_num matches the db" );

$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 Could not UPDATE Reservation");
}
logextra( "Was able to update the status of $ep_num from $current_status to $status" );

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';");
?>
