<?php
declare(strict_types=1);

$ENV = ENV::go();

$UserID = $LoggedUser['ID'];
$Purchase = "10,000 $ENV->BONUS_POINTS";

$GiB = 1024*1024*1024;
$Cost = 150.0 * $GiB;

$DB->query("
  SELECT Uploaded
  FROM users_main
  WHERE ID = $UserID");
  
if ($DB->has_results()) {
    list($Upload) = $DB->next_record();

    if ($Upload >= $Cost) {
        $DB->query("
          UPDATE users_main
          SET BonusPoints = BonusPoints + 10000,
            Uploaded = Uploaded - $Cost
          WHERE ID = $UserID");

        $DB->query("
          UPDATE users_info
          SET AdminComment = CONCAT('".sqltime()." - $Purchase from the store\n\n', AdminComment)
          WHERE UserID = $UserID");
          
        $Cache->delete_value('user_info_heavy_'.$UserID);
        $Cache->delete_value('user_stats_'.$UserID);
        $Worked = true;
    } else {
        $Worked = false;
        $ErrMessage = "Not enough upload";
    }
}

View::show_header('Store'); ?>
<div>
  <h2>Purchase
    <?echo $Worked?"Successful":"Failed"?>
  </h2>
  <div class="box">
    <p>
      <?echo $Worked?("You purchased ".$Purchase):("Error: ".$ErrMessage)?>
    </p>
    <p>
      <a href="/store.php">Back to Store</a>
    </p>
  </div>
</div>
<?php View::show_footer();
