<?php

namespace Planaday\Database\SQLDatabaseControllers\Location;

use ArrayAccess;
use Countable;
use Planaday\Database\SQLDatabaseControllers\Database;
use Planaday\Entities\Location\DTO\Location;
use Planaday\Entities\Location\LocationDatabase;
use Planaday\Utility\DataGrouping\Collection;
use Traversable;

class SQLLocationDatabase implements LocationDatabase, Database {
	public function __construct(
		protected LocationTable $table
	) {}

	public function setLocation( Location $location ) :void{
		$this->table->setLocation($location);
	}

	public function getLocationById( string $id ): ?Location {
		return $this->table->getLocationById($id);
	}

	public function setAll(  ArrayAccess&Traversable&Countable $locations, bool $wipeDB = false ): void {
		$this->table->setAll($locations, $wipeDB);
	}

	public function getAll(): ArrayAccess&Traversable&Countable {
		return $this->table->getAll();
	}

	public function getCreateTableQuery( bool $force = false ): string {
		$outputQuery = '';

		if (!$this->table->isTableValid() || $force) $outputQuery .= $this->table->getDropTableQuery() . $this->table->getCreateTableQuery();

		return $outputQuery;
	}

	public function getDropTableQuery(): string {
		return $this->table->getDropTableQuery();
	}

	public function isDatabaseValid(): bool {
		return $this->table->isTableValid();
	}

	public function getCountAll(): int {
		return $this->table->getCountAll();
	}

	public function getAllRaw(): ArrayAccess&Traversable&Countable {
		return $this->table->getAllRaw();
	}

	/**
	 * @return Collection<string>
	 */
	public function getAllCities(): Collection {
		return $this->table->getAllCities();
	}

	public function rebuildTables(): void {
		$this->table->rebuildTable();
	}
}