<?php

namespace Planaday\Database\SQLDatabaseControllers\DayPart;

use ArrayAccess;
use Countable;
use Planaday\Database\Abstraction\SQLQuery\ColumnTypeMap;
use Planaday\Database\Abstraction\SQLQuery\QueryBuilding\SQLColumn;
use Planaday\Database\Abstraction\SQLQuery\SQLFactory;
use Planaday\Database\SQLDatabaseControllers\LinkingTable;
use Planaday\Database\SQLDatabaseControllers\Location\LocationColumnEnum;
use Planaday\Database\SQLDatabaseControllers\Location\LocationTable;
use Planaday\Platform\PlatformAbstraction\DatabaseInterface;
use Traversable;

class DayPartLocationLink extends LinkingTable {
	public const DAY_PART_LOCATION_LINK_TABLE = 'day_part_location_link';
	protected SQLColumn $columnDayPart;
	protected SQLColumn $columnLocation;

	public function __construct( DatabaseInterface $querySender, SQLFactory $sql, ColumnTypeMap $columnTypeMap ) {
		$this->columnDayPart = $sql->table(
			$querySender->convertTableString(DayPartTable::DAY_PART_TABLE)
		)->getColumn(
			DayPartColumnEnum::ID
		);

		$this->columnLocation = $sql->table(
			$querySender->convertTableString(LocationTable::LOCATION_TABLE)
		)->getColumn(
			LocationColumnEnum::ID
		);

		parent::__construct(
			$querySender,
			$sql,
			self::DAY_PART_LOCATION_LINK_TABLE,
			$this->columnDayPart,
			$this->columnLocation,
			$columnTypeMap
		);
	}

	/**
	 * @param string $dayPartId
	 *
	 * @return ArrayAccess<int|string, string>&Traversable<int|string, string>&Countable
	 */
	public function getLocationsForDayPart(string $dayPartId): ArrayAccess&Traversable&Countable {
		return $this->getConnections($dayPartId, $this->columnDayPart, $this->columnLocation);
	}

	/**
	 * @param string $dayPartId
	 * @param ArrayAccess<int|string, string>&Traversable<int|string, string>&Countable $locationIds
	 *
	 * @return void
	 */
	public function setLocationsForDayPart(string $dayPartId, ArrayAccess&Traversable&Countable $locationIds): void {
		$this->setConnections($dayPartId, $locationIds, $this->columnDayPart, $this->columnLocation);
	}
}