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

date_default_timezone_set('UTC');

$current_time = time();
$total_twt_shows = 300;
$twt_duration = 232812;
$start_date_twt = "2005-09-19";
$rename_date_hpr = "2007-12-31";

// --------------------------------------------
// Get the number of published shows

$ep_retrieve = "SELECT max(date) as latest_episode_release_date, max(id) as latest_episode_id from eps WHERE eps.date <= UTC_DATE();";
if ($result = mysqli_query($connection, $ep_retrieve)) {
  $data=mysqli_fetch_assoc($result);
  $latest_episode_release_date = $data['latest_episode_release_date'];
  $latest_episode_id = intval( $data['latest_episode_id'] );
}

// --------------------------------------------
// Shows in the Future Feed 

$ep_retrieve = "SELECT COUNT( DISTINCT id ) as num_future_shows FROM eps WHERE eps.date > UTC_DATE() AND duration <> 0";
if ($result = mysqli_query($connection, $ep_retrieve)) {
  $data=mysqli_fetch_assoc($result);
  $num_future_shows = intval( $data['num_future_shows'] );
}

// --------------------------------------------
// Shows in the Reserve Queue

$ep_retrieve = "SELECT COUNT(*) as num_reserve_shows FROM reservations WHERE status='RESERVE_SHOW_SUBMITTED'";
if ($result = mysqli_query($connection, $ep_retrieve)) {
  $data=mysqli_fetch_assoc($result);
  $num_reserve_shows = intval( $data['num_reserve_shows'] );
}

// --------------------------------------------
// Get the number of hosts

$ep_retrieve = "SELECT COUNT( DISTINCT hostid ) as num_hosts FROM hosts WHERE valid = '1'";
if ($result = mysqli_query($connection, $ep_retrieve)) {
  $data=mysqli_fetch_assoc($result);
  $num_hosts = intval( $data['num_hosts'] );
}

// --------------------------------------------
// Get the next free slot

$next_free_slot = "https://repo.anhonesthost.net/HPR/hpr_hub/issues/71";

// --------------------------------------------
// playtime
$ep_retrieve = "SELECT SUM(duration) as total_playtime_all_hpr_shows_seconds FROM `eps`";
if ($result = mysqli_query($connection, $ep_retrieve)) {
  $data=mysqli_fetch_assoc($result);
  $total_playtime_all_hpr_shows_seconds = intval( $data['total_playtime_all_hpr_shows_seconds'] );
  $total_playtime_all_shows_seconds = $twt_duration + $total_playtime_all_hpr_shows_seconds;
}

$total_playtime_all_shows_human_readable = convertSecToTime( $total_playtime_all_shows_seconds );

// --------------------------------------------
// Comments

$unprocessed_comments = iterator_count(new FilesystemIterator("$comment_directory", FilesystemIterator::SKIP_DOTS));

// --------------------------------------------
// Calculations

$total_released_shows = $latest_episode_id + $total_twt_shows;
$total_submitted_shows = $latest_episode_id + $total_twt_shows + $num_future_shows + $num_reserve_shows;

$project_age_seconds = strtotime( $start_date_twt ) - $current_time;
$project_age =  convertSecToTime( $project_age_seconds );


// --------------------------------------------
// Display the workflow

$workflow = array();

$ep_retrieve = "SELECT status, COUNT(*) AS total FROM reservations WHERE status != 'RESERVE_SHOW_SUBMITTED' GROUP BY status;";
if ($result = mysqli_query($connection, $ep_retrieve)) {
  while ($row = mysqli_fetch_array($result)) {
    $workflow[ $row['status'] ] = $row['total'];
  }
}

// --------------------------------------------
// Shows in the Reserve Queue

$ep_retrieve = "SELECT COUNT(*) AS num_unprocessed_shows FROM reservations WHERE status!='RESERVE_SHOW_SUBMITTED' AND status!='REQUEST_UNVERIFIED'";
if ($result = mysqli_query($connection, $ep_retrieve)) {
  $data=mysqli_fetch_assoc($result);
  $num_unprocessed_shows = intval( $data['num_unprocessed_shows'] );
}

// --------------------------------------------
// Display the results

$arr = array(
  'stats_generated' => $current_time,
  'start_date_twt' => $start_date_twt,
  'rename_date_hpr' => $rename_date_hpr,
  'latest_episode_release_date' => $latest_episode_release_date,
  'project_age' => $project_age,
  'latest_episode_id' => $latest_episode_id,
  'total_twt_shows' => $total_twt_shows,
  'total_released_shows' => $total_released_shows,
  'num_future_shows' => $num_future_shows,
  'num_reserve_shows' => $num_reserve_shows,
  'total_submitted_shows' => $total_submitted_shows,
  'num_hosts' => $num_hosts,
  'days_to_next_free_slot' => "todo",
  'total_playtime_all_shows_seconds' => $total_playtime_all_shows_seconds,
  'total_playtime_all_shows_human_readable' => $total_playtime_all_shows_human_readable,
  'next_free_slot' => $next_free_slot,
  'unprocessed_comments' => $unprocessed_comments,
  'num_unprocessed_shows' => $num_unprocessed_shows,
  'show_processing_workflow' => $workflow
);

header('Content-Type: application/json');
header("Content-disposition: inline; filename=hpr_stats.json");

echo json_encode($arr);

mysqli_close($connection);
?>

