<?php

	use Reflect\{Response, Path, Call};
	use Reflect\Rules\{Ruleset, Rules, Type};

	use VLW\API\Endpoints;
	use VLW\Database\Database;
	use VLW\Database\Tables\Coffee\{CoffeeTable, StatsTable};

	require_once Path::root("src/API/Endpoints.php");
	require_once Path::root("src/Database/Database.php");
	require_once Path::root("src/Database/Tables/Coffee/Stats.php");
	require_once Path::root("src/Database/Tables/Coffee/Coffee.php");

	class POST_CoffeeStats extends Database {
		protected Ruleset $ruleset;

		public function __construct() {
			$this->ruleset = new Ruleset(strict: true);
			$this->ruleset->validate_or_exit();

			parent::__construct();
		}

		public function main(): Response {
			$truncate = $this->db->execute_query("DELETE FROM `" . StatsTable::NAME . "`");

			// Add a dummy row to run the MariaDB INSERT AFTER Trigger on the coffee database table
			$insert = $this->db->for(CoffeeTable::NAME)->insert([CoffeeTable::ID->value => 0]);
			// Remove the dummy row
			$remove = $this->db->for(CoffeeTable::NAME)->where([CoffeeTable::ID->value => 0])->delete();

			return $truncate && $insert && $remove ? new Response() : new Response("Error", 500);
		}
	}