<?php
session_start();
include("serverConnect.php");
include("functions.php");

$displayError = true;

if (isset($_GET['territory']) && isset($_GET['building'])){
    //TODO : check everything
    
    $buildingId = intval($_GET['building']);
    $territoryId = intval($_GET['territory']);
    
    $building = getBuildingsList()[$buildingId];
	
    $req = $bdd->prepare('INSERT INTO cBuildingConstructions (territory, type, timeFinished) VALUES(?, ?, ?)');
    $req->execute(array($territoryId, $buildingId, time() + $building["construction"]["time"]));
    
    foreach ($building["construction"]["resources"] as $r => $amount) {
        removeResourcesInRegion(getRegionFromTerritory($territoryId), getResourceId($r), $amount);
    }
    
    $displayError = false;
}

if (!$displayError){
	header("Location:region.php");
	exit;
}
else {
    echo "FAIL";
}
?>