<?php

namespace Planaday\Database\MySQL\QueryBuilding;

use Planaday\Database\Abstraction\SQLQuery\QueryBuilding\SQLColumn;
use Planaday\Database\Abstraction\SQLQuery\QueryBuilding\SQLJoin;
use Planaday\Database\Abstraction\SQLQuery\QueryBuilding\SQLTable;
use Planaday\Database\Abstraction\SQLQuery\QueryBuilding\SQLUseInterfaces\SQLFromable;

class MySQLJoin implements SQLJoin {
	protected string $joinType;

	/**
	 * @param SQLFromable $left
	 * @param SQLTable $right
	 * @param SQLColumn $leftColumn
	 * @param SQLColumn $rightColumn
	 * @param bool $includeAllLeft
	 * @param bool $includeAllRight
	 */
	public function __construct(
		protected SQLFromable $left,
		protected SQLTable $right,
		protected SQLColumn $leftColumn,
		protected SQLColumn $rightColumn,
		bool $includeAllLeft = false,
		bool $includeAllRight = false
	) {
		switch ($includeAllLeft + ($includeAllRight*2)){
			case 0:
				$this->joinType = ' INNER';
				break;
			case 1:
				$this->joinType = ' LEFT';
				break;
			case 2:
				$this->joinType = ' RIGHT';
				break;
			case 3:
				$this->joinType = ' FULL OUTER';
				break;
		}
	}


	function getFromQuery(): string {
		return $this->left->getFromQuery() . $this->joinType . ' JOIN ' . $this->right->getFromQuery() .
		       ' ON ' . $this->leftColumn->getFullColumnIdentifier() . '=' . $this->rightColumn->getFullColumnIdentifier();
	}
}