<?php

	use VLW\Database\Models\Search\Search;
	use VLW\Database\Tables\Search\{Search as SearchTable, SearchTypeEnum};

	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");

	const GET_KEY_QUERY    = "q";
	const LIMIT_RESULTS    = 10;
	const MIN_QUERY_LENGTH = 2;

	$search = new class extends Search {
		public readonly string $query;
		public readonly array $results;
		
		public function __construct() {
			$this->query = $_GET[GET_KEY_QUERY] ?? "";
			$this->results = strlen($this->query) >= MIN_QUERY_LENGTH ? parent::query($this->query, limit: LIMIT_RESULTS) : [];
		}
	}

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

				<?php foreach (SearchTypeEnum::names() as $type): ?>
					<?php $type = SearchTypeEnum::fromName($type); ?>
					<option value="<?= $type->name ?>"><?= ucfirst(strtolower($type->name)) ?></option>
				<?php endforeach; ?>

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

<?php if (isset($_GET[GET_KEY_QUERY])): ?>
	
	<?php if ($search->results): ?>
		<section class="stats">
			<p><?= count($search->results) ?> result(s)</p>
			<a href="/search?query=<?= $search->query ?>"><button class="inline solid">
				<?= VV::embed("public/assets/media/icons/search.svg") ?>
				<p>Advanced search</p>
				<?= VV::embed("public/assets/media/icons/chevron.svg") ?>
			</button></a>
		</section>

		<?php foreach ($search->results as $result): ?>
			<section class="result" data-id="<?= $result->id ?>">
				<a href="<?= $result->href ?>"><button class="inline">
					<div>
						<h3><?= $result->title ?></h3>
						<p><?= $result->text ?></p>
					</div>
					<?= VV::embed("public/assets/media/icons/chevron.svg") ?>
				</button></a>
			</section>
		<?php endforeach; ?>

	<?php else: ?>
		<?php switch (strlen($search->query)): default: ?>
				<section class="stats">
					<p>0 result(s)</p>
					<a href="/search?query=<?= $search->query ?>"><button class="inline solid">
						<?= VV::embed("public/assets/media/icons/search.svg") ?>
						<p>Advanced search</p>
						<?= VV::embed("public/assets/media/icons/chevron.svg") ?>
					</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("public/assets/media/icons/search.svg") ?>
					<p>Start typing to search</p>
				</section>
			<?php break; ?>

			<?php case (MIN_QUERY_LENGTH - 1): ?>
				<section class="center">
					<?= VV::embed("public/assets/media/icons/search.svg") ?>
					<p>Almost there, type at least two letters to search</p>
				</section>
			<?php break; ?>

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

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