<?php

/** @noinspection SqlAggregates */
class Tournament
{
	private $id;
	private $name;
	private $size;
	private $place;
	private $price;
	private $description;
	private $date_start, $date_end;
	private $date_inscription;
	private $date_solutions;
	private $date_syntheses;
	private $final;
	private $organizers = [];
	private $year;

	private function __construct()
	{
	}

	public static function fromId($id)
	{
		global $DB;
		$req = $DB->prepare("SELECT * FROM `tournaments` WHERE `id` = ?;");
		$req->execute([htmlspecialchars($id)]);
		$data = $req->fetch();

		if ($data === false)
			return null;

		$tournament = new Tournament();
		$tournament->fill($data);
		return $tournament;
	}

	public static function fromName($name)
	{
		global $DB, $YEAR;
		$req = $DB->prepare("SELECT * FROM `tournaments` WHERE `name` = ? AND `year` = $YEAR;");
		$req->execute([htmlspecialchars($name)]);
		$data = $req->fetch();

		if ($data === false)
			return null;

		$tournament = new Tournament();
		$tournament->fill($data);
		return $tournament;
	}

	public static function getFinalTournament()
	{
		global $DB, $YEAR;
		$req = $DB->query("SELECT * FROM `tournaments` WHERE `final` AND `year` = $YEAR;");
		$data = $req->fetch();

		if ($data === false)
			return null;

		$tournament = new Tournament();
		$tournament->fill($data);
		return $tournament;
	}

	public static function getAllTournaments($include_final = true, $only_future = false)
	{
		global $DB, $YEAR;
		$sql = "SELECT * FROM `tournaments` WHERE ";
		if (!$include_final)
			$sql .= "`final` = 0 AND ";
		if ($only_future)
			$sql .= "`date_start` > CURRENT_DATE AND ";
		$sql .= "`year` = $YEAR ORDER BY `date_start`, `name`;";
		$req = $DB->query($sql);

		$tournaments = [];

		while (($data = $req->fetch()) !== false) {
			$tournament = new Tournament();
			$tournament->fill($data);
			$tournaments[] = $tournament;
		}

		return $tournaments;
	}

	private function fill($data)
	{
		$this->id = $data["id"];
		$this->name = $data["name"];
		$this->size = $data["size"];
		$this->place = $data["place"];
		$this->price = $data["price"];
		$this->description = $data["description"];
		$this->date_start = $data["date_start"];
		$this->date_end = $data["date_end"];
		$this->date_inscription = $data["date_inscription"];
		$this->date_solutions = $data["date_solutions"];
		$this->date_syntheses = $data["date_syntheses"];
		$this->final = $data["final"] == true;
		$this->year = $data["year"];

		global $DB;
		$req = $DB->prepare("SELECT `organizer` FROM `organizers` WHERE `tournament` = ?;");
		$req->execute([$this->id]);

		while (($data = $req->fetch()) !== false)
			$this->organizers[] = User::fromId($data["organizer"]);
	}

	public function getId()
	{
		return $this->id;
	}

	public function getName()
	{
		return $this->name;
	}

	public function setName($name)
	{
		global $DB;
		$this->name = $name;
		$DB->prepare("UPDATE `tournaments` SET `name` = ? WHERE `id` = ?;")->execute([$name, $this->id]);
	}

	public function getSize()
	{
		return $this->size;
	}

	public function setSize($size)
	{
		global $DB;
		$this->size = $size;
		$DB->prepare("UPDATE `tournaments` SET `size` = ? WHERE `id` = ?;")->execute([$size, $this->id]);
	}

	public function getPlace()
	{
		return $this->place;
	}

	public function setPlace($place)
	{
		global $DB;
		$this->place = $place;
		$DB->prepare("UPDATE `tournaments` SET `place` = ? WHERE `id` = ?;")->execute([$place, $this->id]);
	}

	public function getPrice()
	{
		return $this->price;
	}

	public function setPrice($price)
	{
		global $DB;
		$this->price = $price;
		$DB->prepare("UPDATE `tournaments` SET `price` = ? WHERE `id` = ?;")->execute([$price, $this->id]);
	}

	public function getDescription()
	{
		return $this->description;
	}

	public function setDescription($desc)
	{
		global $DB;
		$this->description = $desc;
		$DB->prepare("UPDATE `tournaments` SET `description` = ? WHERE `id` = ?;")->execute([$desc, $this->id]);
	}

	public function getStartDate()
	{
		return $this->date_start;
	}

	public function setStartDate($date)
	{
		global $DB;
		$this->date_start = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_start` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function getEndDate()
	{
		return $this->date_end;
	}

	public function setEndDate($date)
	{
		global $DB;
		$this->date_end = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_end` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function getInscriptionDate()
	{
		return $this->date_inscription;
	}

	public function setInscriptionDate($date)
	{
		global $DB;
		$this->date_inscription = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_inscription` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function getSolutionsDate()
	{
		return $this->date_solutions;
	}

	public function setSolutionsDate($date)
	{
		global $DB;
		$this->date_solutions = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_solutions` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function getSynthesesDate()
	{
		return $this->date_syntheses;
	}

	public function setSynthesesDate($date)
	{
		global $DB;
		$this->date_syntheses = $date;
		$DB->prepare("UPDATE `tournaments` SET `date_syntheses` = ? WHERE `id` = ?;")->execute([$date, $this->id]);
	}

	public function isFinal()
	{
		return $this->final;
	}

	public function setFinal($final)
	{
		global $DB;
		$this->final = $final;
		$DB->prepare("UPDATE `tournaments` SET `final` = ? WHERE `id` = ?;")->execute([$final, $this->id]);
	}

	public function getAllTeams()
	{
		global $DB, $YEAR;
		if ($this->final)
			$req = $DB->query("SELECT `id` FROM `teams` WHERE `final_selection` AND `year` = $YEAR;");
		else
			$req = $DB->query("SELECT `id` FROM `teams` WHERE `tournament` = $this->id AND `year` = $YEAR;");

		$teams = [];

		while (($data = $req->fetch()) !== false)
			$teams[] = Team::fromId($data["id"]);

		return $teams;
	}

	public function getOrganizers()
	{
		return $this->organizers;
	}

	public function organize($user_id)
	{
		foreach ($this->organizers as $organizer) {
			if ($organizer->getId() == $user_id)
				return true;
		}

		return false;
	}

	public function addOrganizer(User $user)
	{
		global $DB;

		$this->organizers[] = $user;

		$req = $DB->prepare("INSERT INTO `organizers`(`organizer`, `tournament`) VALUES(?, ?);");
		$req->execute([$user->getId(), $this->id]);
	}

	public function clearOrganizers()
	{
		global $DB;

		$this->organizers = [];

		$req = $DB->prepare("DELETE FROM `organizers` WHERE `tournament` = ?;");
		$req->execute([$this->id]);
	}

	public function getYear()
	{
		return $this->year;
	}

	public function getAllDocuments($team_id = -1)
	{
		global $DB;

		$req = $DB->query("SELECT * FROM `documents` AS `t1` "
			. "INNER JOIN (SELECT `user`, `type`, `tournament`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `documents` GROUP BY `tournament`, `team`, `type`, `user`) `t2` "
			. "ON `t1`.`user` = `t2`.`user` AND `t1`.`type` = `t2`.`type` AND `t1`.`tournament` = `t2`.`tournament` "
			. "WHERE `t1`.`uploaded_at` = `t2`.`last_upload` AND `t1`.`tournament` = $this->id " . ($team_id == -1 ? "" : "AND `t1`.`team` = $team_id") . " ORDER BY `t1`.`team`, `t1`.`type`;");

		$docs = [];

		while (($data = $req->fetch()) !== false)
			$docs[] = Document::fromData($data);

		return $docs;
	}

	public function getAllSolutions($team_id = -1)
	{
		global $DB;

		$req = $DB->query("SELECT * FROM `solutions` AS `t1` "
			. "INNER JOIN (SELECT `team`, `problem`, `tournament`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `solutions` GROUP BY `tournament`, `team`, `problem`) `t2` "
			. "ON `t1`.`team` = `t2`.`team` AND `t1`.`problem` = `t2`.`problem` AND `t1`.`tournament` = `t2`.`tournament` "
			. "WHERE `t1`.`uploaded_at` = `t2`.`last_upload` AND `t1`.`tournament` = $this->id " . ($team_id == -1 ? "" : "AND `t1`.`team` = $team_id") . " ORDER BY `t1`.`team`, `t1`.`problem`;");

		$sols = [];

		while (($data = $req->fetch()) !== false)
			$sols[] = Solution::fromData($data);

		return $sols;
	}

	public function getAllSyntheses($team_id = -1)
	{
		global $DB;

		$req = $DB->query("SELECT * FROM `syntheses` AS `t1` "
			. "INNER JOIN (SELECT `team`, `dest`, `tournament`, MAX(`uploaded_at`) AS `last_upload`, COUNT(`team`) AS `version` FROM `syntheses` GROUP BY `tournament`, `team`, `dest`) `t2` "
			. "ON `t1`.`team` = `t2`.`team` AND `t1`.`dest` = `t2`.`dest` AND `t1`.`tournament` = `t2`.`tournament` "
			. "WHERE `t1`.`uploaded_at` = `t2`.`last_upload` AND `t1`.`tournament` = $this->id " . ($team_id == -1 ? "" : "AND `t1`.`team` = $team_id") . " ORDER BY `t1`.`team`, `t1`.`dest`;");

		$syntheses = [];

		while (($data = $req->fetch()) !== false)
			$syntheses[] = Synthesis::fromData($data);

		return $syntheses;
	}
}