<?php

namespace App\ImportedOrder;

/**
 * Contains info about the current supplier.
 * @author Gabriel
 */
class Supplier extends EntityAbstract
{

	/**
	 * The supplier id in TINA.
	 * @var Int
	 */
	public $idTina;

	/**
	 * The supplier id in the external system.
	 * @var String
	 */
	public $idSystem;

	/**
	 * The name (company/alias) of the current supplier.
	 * @var String
	 */
	public $name;

	/**
	 * A list of codes for the current supplier (IATA, numeric IATA, etc). This is an associative
	 * array with the key as the supplier code type.
	 * @var ?array
	 */
	public ?array $codes = null;

	/**
	 * The fiscal code of the current supplier.
	 * @var String
	 */
	public $fiscalCode;

	/**
	 * Returns a list of internal class attributes that can be copied when merging orders.
	 * @return Array
	 */
	protected function getMergingFields() :array
	{
		return [ 'idTina', 'idSystem', 'name', 'codes', 'fiscalCode' ];
	}


	/**
	 * Sets a new code for the current supplier (you have to provide the code name and the code value).
	 * @param String $code
	 * @param String $value
	 */
	public function addCode($code, $value)
	{
		$this->codes[$code] = $value;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\ImportedOrder\EntityAbstract::validateEntityFields()
	 */
	protected function validateEntityFields(\App\MessagesCollector $messagesCollector): void
	{

		// checks idTina
		if ( $this->idTina and (!is_numeric($this->idTina) or $this->idTina<0) ) {
			$messagesCollector->addInfo('The TINA id is invalid - it should be a positive integer.');
		}

		// validate codes
		if ($this->codes) {
			$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
			foreach ($this->codes as $code=>$val) {
				if (!$CFG['travel']['standardCodes'][$code]) {
					$messagesCollector->addInfo("The code type `{$code}` is invalid / not configured.");
				}
			}
		}

	}


	/**
	 * Gets the entity match object.
	 * @param String $idExternalSystem
	 * @return \App\ImportedOrder\EntityMatch\Supplier
	 */
	public function getPopulatedMatchObject($idExternalSystem)
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		$matchObj = new \App\ImportedOrder\EntityMatch\Supplier('intersect');
		if ($this->idTina) {
			$matchObj->setId($this->idTina);
		}
		if ($idExternalSystem and $this->idSystem) {
			$matchObj->setIdInExtSys(htmlClean($this->idSystem), $idExternalSystem);
		}
		if ($this->fiscalCode) {
			$matchObj->setFiscalCode(htmlClean($this->fiscalCode));
		}
		if ($this->name) {
			$matchObj->setAlias(htmlClean($this->name), true);
		}
		if ($this->codes) {
			foreach($this->codes as $codeName=>$codeValue) {
				if ($CFG['travel']['standardCodes'][$codeName] and $codeValue) {
					$matchObj->setCode($codeValue, $codeName);
				}
			}
		}

		return $matchObj;

	}


	/**
	 * Matches the entity by its fields - can also check for matches in the external
	 * systems associations (if $idExternalSystem is set).
	 * @param Int $idExternalSystem - if set, it is attempted to match the user by the manual associations;
	 * @return Int - the entity id, if matched; 0/blank if no entity was matched
	 */
	public function getMatchedId($idExternalSystem)
	{

		$matchObj = $this->getPopulatedMatchObject($idExternalSystem);

		$matchObj->search();
		if ($matchObj->getResultsCount()==1) {
			return $matchObj->getFirstMatchId();
		}

		return 0;

	}


}