<?php

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

	use VLW\API\API;
	use VLW\Helpers\{
		Forgejo,
		GenerateSearch,
		GenerateTimeline
	};

	require_once Path::root("src/API/API.php");
	require_once Path::root("src/Helpers/Forgejo.php");
	require_once Path::root("src/Helpers/GenerateSearch.php");
	require_once Path::root("src/Helpers/GenerateTimeline.php");

	enum ServiceEnum: string {
		use xEnum;

		case ALL      = "all";
		case SEARCH   = "search";
		case FORGEJO  = "forgejo";
		case TIMELINE = "timeline";
	}

	final class GET_Update extends API {
		private const KEY_SERVICE = "service";

		public function __construct() {
			parent::__construct(new Ruleset()->GET([
				new Rules(self::KEY_SERVICE)
					->type(Type::ENUM, ServiceEnum::values())
					->default(ServiceEnum::ALL->value)
			]));
		}

		public function main(): Response {
			switch ($_GET[self::KEY_SERVICE]) {
				case ServiceEnum::FORGEJO->value:
					return new Response($this->update_forgejo());

				case ServiceEnum::SEARCH->value:
					return new Response($this->update_search());

				case ServiceEnum::TIMELINE->value:
					return new Response($this->update_timeline());

				case ServiceEnum::ALL->value:
				default:
					return new Response(
						$this->update_timeline() &&
						$this->update_search() &&
						$this->update_forgejo()
					);
			}
		}
		
		private function update_timeline(): bool {
			return new GenerateTimeline()->generate();
		}
		
		private function update_search(): bool {
			return new GenerateSearch()->generate();
		}

		private function update_forgejo(): bool {
			return new Forgejo()->update();
		}
	}