<?php

namespace Planaday\Database\SQLDatabaseControllers;

use Planaday\Database\Abstraction\DatabaseHandler;
use Planaday\Database\SQLDatabaseControllers\Booking\SQLBookingDatabase;
use Planaday\Database\SQLDatabaseControllers\Course\SQLCourseDatabase;
use Planaday\Database\SQLDatabaseControllers\DayPart\SQLDayPartDatabase;
use Planaday\Database\SQLDatabaseControllers\Location\SQLLocationDatabase;
use Planaday\Utility\Debug\Log\Logging;

class SQLDatabaseHandler implements DatabaseHandler {
	use Logging;

	/** @var Database[] */
	protected array $databases;

	function __construct(
		SQLLocationDatabase $locationDatabase,
		SQLDayPartDatabase $dayPartDatabase,
		SQLCourseDatabase $courseDatabase,
		SQLBookingDatabase $bookingDatabase
	) {
		$this->databases = [
			$locationDatabase,
			$dayPartDatabase,
			$courseDatabase,
			$bookingDatabase
		];
	}

	public function areDatabasesValid():bool {
		foreach ($this->databases as $database) {
			if (!$database->isDatabaseValid()) {
				return false;
			}
		}
		return true;
	}

	public function getCreateDatabaseQueries( bool $forceAllCreation = false ): string {
		$createTableQueries = '';

		foreach ($this->databases as $database) {
			$createTableQueries .= $database->getCreateTableQuery( $forceAllCreation );
		}

		return $createTableQueries;
	}

	public function rebuildDatabases():void{
		foreach ($this->databases as $database) {
			$database->rebuildTables();
		}
	}
}