<?php

	namespace VLW\Helpers;

	use \VV;

	use VLW\Database\Database;
	use VLW\Database\Models\Work\{Work, Timeline};
	use VLW\Database\Tables\Work\Timeline as TimelineTable;

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

	class GenerateTimeline {
		private readonly Database $db;

		public function __construct() {
			$this->db = new Database();
		}

		public function generate(): bool {
			$this->truncate();

			foreach (Work::all() as $work) {
				if (!Timeline::new($work)) { return false; };
			}

			return true;
		}

		private function truncate(): bool {
			return $this->db->from(TimelineTable::TABLE)->delete();
		}
	}