<?php

	namespace VLW\Database\Models\Search;

	use \VV;

	use VLW\API\Endpoints;
	use VLW\Database\Models\Model;
	use VLW\Database\Tables\Search\{SearchTable, SearchCategoryEnum};

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

	class Search extends Model {
		public function __construct(public readonly string $id) {
			parent::__construct(Endpoints::SEARCH, [
				SearchTable::ID->value => $this->id
			]);
		}

		public static function all(array $params = []): array {
			return array_map(fn(array $item): Search => new Search($item[SearchTable::ID->value]), parent::list(Endpoints::SEARCH, $params));
		}

		public function title(): ?string {
			return $this->get(SearchTable::TITLE->value);
		}

		public function summary(): ?string {
			return $this->get(SearchTable::SUMMARY->value);
		}

		public function category(): ?SearchCategoryEnum {
			return SearchCategoryEnum::tryFromName($this->get(SearchTable::CATEGORY->value));
		}

		public function href(): ?string {
			return $this->get(SearchTable::HREF->value);
		}
	}