<?php

namespace Planaday\Database\MySQL\QueryBuilding\SQLOperators;

use Planaday\Database\Abstraction\SQLQuery\QueryBuilding\SQLOperators\SQLBetween;
use Planaday\Database\Abstraction\SQLQuery\QueryBuilding\SQLUseInterfaces\SQLComparable;
use Planaday\Platform\PlatformAbstraction\DataHandler;

class MySQLBetween implements SQLBetween {
	/**
	 * @param DataHandler $dataHandler
	 * @param SQLComparable $toBeTested the value to be tested
	 * @param string|int|float $valueMin
	 * @param string|int|float $valueMax
	 * @param bool $betweenOrOutside if false, will use NOT BETWEEN
	 */
	public function __construct(
		protected DataHandler $dataHandler,
		protected SQLComparable $toBeTested,
		protected string|int|float $valueMin,
		protected string|int|float $valueMax,
		protected bool $betweenOrOutside = true
	) {}


	function toConditionQuery(): string {
		$output = $this->toBeTested->getCompareIdentifier();
		if(!$this->betweenOrOutside) $output .= ' NOT';
		$output .= ' BETWEEN ';

		if (!is_int($this->valueMin) && !is_float($this->valueMin)){
			$this->valueMin = "'" . $this->dataHandler->escapeSql($this->valueMin) . "'";
		}
		if (!is_int($this->valueMax) && !is_float($this->valueMax)){
			$this->valueMax = "'" . $this->dataHandler->escapeSql($this->valueMax) . "'";
		}

		$output .= $this->valueMin . ' AND ' . $this->valueMax;

		return $output;
	}
}