<?php

namespace Planaday\Database\MySQL\QueryBuilding;

use Planaday\Database\Abstraction\SQLQuery\QueryBuilding\SQLColumn;

class MySQLColumn implements SQLColumn {
	protected bool $orderDirection = true;

	/**
	 * @param string $column
	 * @param string $table
	 * @param string|null $alias
	 */
	public function __construct(
		protected string $column,
		protected string $table,
		protected ?string $alias = null
	) {}

	public function getColumn(): string {
		return $this->column;
	}

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

	function toSelectQuery(): string {
		return $this->table . '.' . $this->column . (isset($this->alias) ? ' AS ' . $this->alias : '');
	}

	function setAlias( string $alias ): void{
		$this->alias = $alias;
	}

	function getFullColumnIdentifier():string {
		return $this->table . '.' . $this->column;
	}

	function setOrderDirection(bool $ascending): SQLColumn{
		$this->orderDirection = $ascending;
		return $this;
	}

	function getOrderQuery():string {
		return $this->getFullColumnIdentifier() . (($this->orderDirection ?? true) ? ' ASC' : ' DESC');
	}

	function getCompareIdentifier(): string {
		return $this->getFullColumnIdentifier();
	}
}