<?php

namespace Entity;

class AriaChatMessage extends \App\Entity\GeneralAbstract
{
	/**
	 * Indicate if operations for the current entity must be done inside a transaction or not.
	 * @var bool
	 */
	protected $saveInsideTransaction = true;

	/**
	 * Set the DB table name for this entity
	 */
	protected function setTableName(): void
	{
		global $DBTAriaChatMessages;
		$this->tableName = $DBTAriaChatMessages;
	}

	/**
	 * Invalidates the default "update" operation.
	 */
	static public function update($id)
	{
		throw new \Exception('Not developed.');
	}

	/**
	 * Invalidates the default "update" operation.
	 */
	static public function delete($id)
	{
		throw new \Exception('Not developed.');
	}

	/**
	 * Retrieves the attribute corresponding to the given key.
	 */
	protected function getAttribute($var)
	{

		switch ($var) {

			// chat
			case 'chat': {
				return \Entity\AriaChat::select($this->fields['idChat'], $this->selectForUpdate);
			}

			case 'parentMessage': {
				return \Entity\AriaChat::select($this->fields['idParentMessage'], $this->selectForUpdate);
			}

			// returns an array with the report content
			case 'reportContent': case 'requestLog': case 'responseLog':case 'tokens': {
				if (!is_array($this->fields[$var])) {
					$this->fields[$var] = $this->fields[$var] ? json_decode($this->fields[$var], true) : null ;
				}
				return $this->fields[$var];
			}

			// the rest of the fields (most of them are just copied)
			default: {
				return parent::getAttribute($var);
			}
		}
	}

	protected function setAttribute($var, $value)
	{
		// check that operation is okay
		if ($this->operation!='insert') {
			throw new \Exception("You cannot use __set magic method on `{$this->operation}` operation in `".__CLASS__."` class.");
		}

		switch ($var) {
			// normal fields
			case 'idChat': case 'userPrompt':case 'callType':case 'textReply':case 'hidden':case 'idParentMessage':
			case 'requestLog':case 'responseLog':
				$this->fields[$var] = $value;
			break;

			case 'reportContent':case 'requestLog': case 'responseLog':
				if (!is_object($value)) { // TODO: should accept array also?
					throw new \Exception("The `{$var}` key must be set as an object in ".__METHOD__);
				}
				$this->fields[$var] = $value;
			break;

			case 'tokens':
				if (!is_array($value)) {
					throw new \Exception("The `{$var}` key must be set as an array in ".__METHOD__);
				}
				$this->fields[$var] = $value;
			break;

			// the field is not valid
			default: {
				throw new \Exception("The field '{$var}' is not valid for the `".__METHOD__."` entity call.");
			}
		}

	}

	protected function save_filterSaveData(Array $saveData)
	{

		if ($this->operation=='insert') {
			// encode objects/array
			if (key_exists('reportContent', $saveData)) {
				$saveData['reportContent'] = json_encode($saveData['reportContent']);
			}
			if (key_exists('requestLog', $saveData)) {
				$saveData['requestLog'] = json_encode($saveData['requestLog']);
			}
			if (key_exists('responseLog', $saveData)) {
				$saveData['responseLog'] = json_encode($saveData['responseLog']);
			}
			if (key_exists('tokens', $saveData)) {
				$saveData['tokens'] = json_encode($saveData['tokens']);
			}
		}

		return $saveData;
	}

	/**
	 * Eliminates the idAddUser (no such field) but adds addTime.
	 */
	protected function save_finalSaveData(Array $saveData)
	{
		if ($this->operation=='insert') {
			$saveData['addTime'] = DATETIME;
			return $saveData;
		}
		else {
			return parent::save_finalSaveData($saveData);
		}
	}



	/**
	 * (non-PHPdoc)
	 * @see Entity.PartnerContractAbstract::save_postProcess()
	 */
	protected function save_postProcess(Array $saveData)
	{
		// delete cache
		$this->save_updateCache_addEntry('AriaChat', $this->fields['idChat']);

		parent::save_postProcess($saveData);

	}

}
