<?php
//設定檔
require_once dirname(__FILE__)."/../configuration/configFactory.php";
//資料庫核心類
require_once dirname(__FILE__)."/database.php";

class databaseFactory {
	private static ?Database $database = null;
	private function __construct() {}
	
	//創建資料庫
	public static function createDatabase(): Database {
		if (!isset(self::$database) || !self::$database->ping()) {
			$config = configFactory::createConfiguration();

			$dbHost = $config->getConfigValue("data", "dbHost");
			$dbUser = $config->getConfigValue("data", "dbUser");
			$dbPwd = $config->getConfigValue("data", "dbPassword");
			$dbName = $config->getConfigValue("data", "dbName");
			$dbCharset = $config->getConfigValue("data", "dbCharset");

			self::$database = new Database($dbHost, $dbUser, $dbPwd, $dbName, $dbCharset);
			if (!self::$database->ping()) {
				echo "Failed to connect to the database!";
				exit;
			}
		}
		return self::$database;
	}
}
?>