<?php

	namespace VLW\Database\Models\Work;

	use \VV;

	use VLW\API\Endpoints;
	use VLW\Database\Models\Model;
	use VLW\Database\Tables\Work\WorkTable;
	use VLW\Database\Models\Work\{Tag, Action};

	require_once VV::root("src/API/Endpoints.php");
	require_once VV::root("src/Database/Models/Model.php");
	require_once VV::root("src/Database/Models/Work/Tag.php");
	require_once VV::root("src/Database/Tables/Work/Work.php");
	require_once VV::root("src/Database/Models/Work/Action.php");

	class Work extends Model {
		public const DATE_FORMAT = "Y-m-d";

		public function __construct(public readonly string $id) {
			parent::__construct(Endpoints::WORK, [
				WorkTable::ID->value => $this->id
			]);
		}

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

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

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

		public function created(): \DateTimeImmutable {
			return new \DateTimeImmutable($this->get(WorkTable::CREATED->value));
		}

		public function tags(): array {
			return Tag::from($this);
		}

		public function actions(): array {
			return Action::from($this);
		}
	}