<?php

namespace Planaday\Database\SQLDatabaseControllers\Course;

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\Platform\PlatformAbstraction\DatabaseInterface;
use Traversable;

class CourseCompanyLink extends LinkingTable {
	public const COURSE_COMPANY_LINK = 'course_company_link';
	public const COMPANY_TABLE_PLACEHOLDER = "company";
	public const COMPANY_COLUMN_PLACEHOLDER = "id";
	protected SQLColumn $columnCourse;
	protected SQLColumn $columnCompany;

	public function __construct( DatabaseInterface $querySender, SQLFactory $sql, ColumnTypeMap $columnTypeMap ) {
		$this->columnCourse = $sql->table(
			$querySender->convertTableString(CourseTable::COURSE_TABLE)
		)->getColumn(
			CourseColumnEnum::ID
		);

		$this->columnCompany = $sql->table(
			$querySender->convertTableString(self::COMPANY_TABLE_PLACEHOLDER)
		)->getColumn(
			self::COMPANY_COLUMN_PLACEHOLDER
		);

		parent::__construct(
			$querySender,
			$sql,
			self::COURSE_COMPANY_LINK,
			$this->columnCourse,
			$this->columnCompany,
			$columnTypeMap
		);
	}

	/**
	 * @param string $courseId
	 *
	 * @return ArrayAccess<int|string, string>&Traversable<int|string, string>&Countable
	 */
	public function getCompaniesForCourse(string $courseId): ArrayAccess&Traversable&Countable {
		return $this->getConnections($courseId, $this->columnCourse, $this->columnCompany);
	}

	/**
	 * @param string $courseId
	 * @param ArrayAccess<int|string, int>&Traversable<int|string, int>&Countable $companyIds
	 *
	 * @return void
	 */
	public function setCompaniesForCourse(string $courseId, ArrayAccess&Traversable&Countable $companyIds): void {
		$this->setConnections($courseId, $companyIds, $this->columnCourse, $this->columnCompany);
	}

}