<?php

	use VLW\Database\Models\Search\Search;
	use const VLW\{ICONS_DIR, DEFAULT_BUTTON_ICON};
	use VLW\Database\Tables\Search\{SearchTable, SearchCategoryEnum};

	require_once VV::root("src/Consts.php");
	require_once VV::root("src/Database/Tables/Search/Search.php");
	require_once VV::root("src/Database/Models/Search/Search.php");

	$search = new class extends Search {
		public function __construct() {}

		public static function get_query(): ?string {
			return $_GET[SearchTable::QUERY->value] ?? null;
		}

		public static function get_category(): ?SearchCategoryEnum {
			return SearchCategoryEnum::tryFromName($_GET[SearchTable::CATEGORY->value] ?? "");
		}

		public function search(): array {
			return parent::all([SearchTable::QUERY->value => self::get_query()]);
		}
	}

?>
<style><?= VV::css("public/assets/css/pages/search") ?></style>
<section class="search">
	<form>
		<input name="<?= SearchTable::QUERY->value ?>" type="search" placeholder="search vlw.se..." value="<?= $search::get_query() ?>">
		<select name="<?= SearchTable::CATEGORY->value ?>">
			<option value="null">All</option>
			<optgroup label="Categories">

				<?php foreach (SearchCategoryEnum::names() as $category): ?>
					<?php $category = SearchCategoryEnum::fromName($category); ?>
					<option value="<?= $category->name ?>" <?= $search::get_category() === $category ? "selected" : "" ?>><?= ucfirst(strtolower($category->name)) ?></option>
				<?php endforeach; ?>

			</optgroup>
		</select>
		<button type="submit" class="inline solid"><?= VV::embed(ICONS_DIR . "search.svg") ?></button>
	</form>
</section>

<?php if (array_key_exists(SearchTable::QUERY->value, $_GET)): ?>
	
	<?php if ($search->search()): ?>
		<section class="stats">
			<p><?= count($search->search()) ?> result(s)</p>
			<a href="/search?query=<?= $search::get_query() ?>"><button class="inline solid">
				<?= VV::embed(ICONS_DIR . "search.svg") ?>
				<p>Advanced search</p>
				<?= VV::embed(DEFAULT_BUTTON_ICON) ?>
			</button></a>
		</section>

		<?php foreach ($search->search() as $result): ?>
			<section class="result" data-id="<?= $result->id ?>">
				<a href="<?= $result->href() ?>"><button class="inline">
					<div>
						<h2><?= $result->title() ?></h2>
						<p><?= $result->summary() ?></p>
					</div>
					<?= VV::embed(DEFAULT_BUTTON_ICON) ?>
				</button></a>
			</section>
		<?php endforeach; ?>

	<?php else: ?>
		<?php switch (strlen($search::get_query())): default: ?>
				<section class="stats">
					<p>0 result(s)</p>
					<a href="/search?query=<?= $search::get_query() ?>"><button class="inline solid">
						<?= VV::embed(ICONS_DIR . "search.svg") ?>
						<p>Advanced search</p>
						<?= VV::embed(DEFAULT_BUTTON_ICON) ?>
					</button></a>
				</section>
				<section class="center">
					<img src="/assets/media/travolta.gif">
					<p>Nothing to see here, that's a bummer..</p>
				</section>
			<?php break; ?>

			<?php case 0: ?>
				<section class="center">
					<?= VV::embed(ICONS_DIR . "search.svg") ?>
					<p>Start typing to search</p>
				</section>
			<?php break; ?>

			<?php case 1: ?>
				<section class="center">
					<?= VV::embed(ICONS_DIR . "search.svg") ?>
					<p>Almost, type at least two letters to search</p>
				</section>
			<?php break; ?>

		<?php endswitch; ?>
		
	<?php endif; ?>

<?php else: ?>
	<section class="center">
		<?= VV::embed(ICONS_DIR . "search.svg") ?>
		<p>Start typing to search</p>
	</section>
<?php endif; ?>
<script><?= VV::js("public/assets/js/pages/search") ?></script>