<?php

	use VLW\Database\Models\Work\Timeline;

	require_once VV::root("src/Database/Models/Work/Timeline.php");

	const ARG_KEY_LIMIT = "limit";

	$timeline = new class extends Timeline {
		public function __construct() {}

		public static function ordered(?int $limit = null): array {
			$timeline = [];

			foreach (parent::all() as $idx => $item) {
				// Use year as the first dimension
				if (!array_key_exists($item->year, $timeline)) {
					$timeline[$item->year] = [];
				}

				// And month as the second dimension
				if (!array_key_exists($item->month, $timeline[$item->year])) {
					$timeline[$item->year][$item->month] = [];
				}

				// Lastly, day as the third dimension
				if (!array_key_exists($item->day, $timeline[$item->year][$item->month])) {
					$timeline[$item->year][$item->month][$item->day] = [];
				}

				// Append Work instance on Timeline object to the output array by year->month->day
				$timeline[$item->year][$item->month][$item->day][] = $item->work;

				// Bail out here if we've reached the theshold for items to display
				if ($limit && $idx === $limit) {
					return $timeline;
				}
			}

			return $timeline;
		}
	}

?>
<style><?= VV::css("public/assets/css/pages/work/timeline") ?></style>
<section class="git">
	<?= VV::embed("public/assets/media/icons/codeberg.svg") ?>
	<p>This timeline has most but not all of my FOSS software. If you want to see a list of all things I've created for the free software world, check out my repos on Codeberg or Forgejo.</p>
	<div class="buttons">
		<a href="https://codeberg.org/vlw"><button class="inline solid">
			<p>Codeberg</p>
			<?= VV::embed("public/assets/media/icons/chevron.svg") ?>
		</button></a>
		<a href="https://git.vlw.se/explore/repos"><button class="inline">
			<p>Forgejo</p>
			<?= VV::embed("public/assets/media/icons/chevron.svg") ?>
		</button></a>
	</div>
</section>
<section class="timeline">

	<?php // Get year int from key and array of months for current year ?>
	<?php foreach ($timeline::ordered($args[ARG_KEY_LIMIT] ?? null) as $year => $months): ?>
		<div class="year">
			<div class="track">
				<p><?= $year ?></p>
			</div>

			<div class="months">
				<?php // Get month int from key and array of days for current month ?>
				<?php foreach ($months as $month => $days): ?>
					<div class="month">
						<div class="track">
							<?php // Append leading zero to month ?>
							<p><?= sprintf("%02d", $month) ?></p>
						</div>

						<div class="days">
							<?php // Get day int from key and array of items for current day ?>
							<?php foreach ($days as $day => $items): ?>
								<div class="day">
									<div class="track">
										<?php // Append leading zero to day ?>
										<p><?= sprintf("%02d", $day) ?></p>
									</div>

									<div class="items">
										<?php foreach ($items as $work): ?>
											<div class="item">

												<?php // List tags if available ?>
												<?php if ($work->tags()): ?>
													<div class="tags">

														<?php foreach ($work->tags() as $tag): ?>
															<p class="tag <?= $tag->label->name ?>"><?= $tag->label->name ?></p>
														<?php endforeach; ?>

													</div>
												<?php endif; ?>

												<?php if ($work->title): ?>
													<h2><?= $work->title ?></h2>
												<?php endif; ?>

												<p><?= $work->summary ?></p>
												
												<?php if ($work->actions()): ?>
													<div class="actions">

														<?php foreach ($work->actions() as $action): ?>
															<a href="<?= $action->href ?? "/work/{$work->id}" ?>"><button class="inline <?= $action->classlist ?>">
																<?php if ($action->icon_prepend): ?>
																	<?= VV::embed("public/assets/media/icons/" . $action->icon_prepend) ?>
																<?php endif; ?>

																<p><?= $action->text ?></p>

																<?php if ($action->icon_append): ?>
																	<?= VV::embed("public/assets/media/icons/" . $action->icon_append) ?>
																<?php else: ?>
																	<?= VV::embed("public/assets/media/icons/chevron.svg") ?>
																<?php endif; ?>
															</button></a>
														<?php endforeach; ?>

													</div>
												<?php endif; ?>

											</div>
										<?php endforeach; ?>
									</div>

								</div>
							<?php endforeach; ?>
						</div>

					</div>
				<?php endforeach; ?>
			</div>
			
		</div>
	<?php endforeach; ?>

</section>