<?php
require_once "../../base/settings.php";
require_once "../../base/headers.php";
require_once "../../base/database.php";
require_once "../../base/helpers.php";
require_once "./queries.php";

session_name("PHP_SESSION_guestBook");
session_start();

if (isset($_SESSION["user"])) {
  Headers::redirect("../");
  return;
}

$_SESSION["error"] = [];
$_SESSION["message"] = [];

if (!isset($_GET["c"])) {
  Headers::redirect(".");
  return;
}
$code = $_GET["c"];

$db = DB::openConnection();

$stmt = $db->prepare($confirmFetchUserQuery);
$stmt->bindValue(":COD", $code);
$stmt->execute();
$uid = $stmt->fetch(PDO::FETCH_COLUMN);

if ($uid !== false) {
  $stmt = $db->prepare($confirmUserQuery);
  $stmt->bindValue(":UID", $uid);
  $stmt->execute();

  array_push($_SESSION["message"], "Account confirmed, you can now log in!");
} else {
  array_push($_SESSION["error"], "Account could not be confirmed");
}

DB::closeConnection($db);

Headers::redirect("./login");
