<?php

namespace Modules\Reports\AriaReports\Model\GetPivotTableStructure;

class Response extends \Modules\Reports\AriaReports\Model\ResponseAbstract
{

	protected ?string $type = null;

	/**
	 * Report structure details (object).
	 */
	protected ?object $pivotStructure = null;

	protected ?string $displayCurrency = null; // TODO: remove?

	public function getPivotStructure(): ?object
	{
		return $this->pivotStructure;
	}

	protected function processResponseObject(): void
	{

		$output = $this->getRelevantOutput();

		if ($output->type=='message') {
			$firstContent = $output->content[0];
			$this->textMessage = $firstContent->text;
		} elseif ($output->type=='function_call') {

			// load AI output (arguments) and populate PivotTableDefinition object which in turn is stored locally
			$pivotDefObj = new \Modules\Reports\AriaReports\Model\PivotTableDefinition();
			$this->populatePivotDefObject($pivotDefObj, json_decode($output->arguments, true));
			$this->pivotStructure = $pivotDefObj;

		} else {
			throw new \RuntimeException('Unknown output type from the AI.');
		}
	}


	protected function populatePivotDefObject(\Modules\Reports\AriaReports\Model\PivotTableDefinition $pivotDefObj, array $aiGeneratedPivotStructure): void
	{

		if ($aiGeneratedPivotStructure['filters']) {
			foreach($aiGeneratedPivotStructure['filters'] as $filter) {
				$operation = $filter['operation'];
				$value = $filter['value'];
				if ($filter['value']=='null') {
					if ($operation=='=') {
						$operation = 'IS NULL';
					} else {
						$operation = 'IS NOT NULL';
					}
					$value= null;
				}
				$pivotDefObj->addFilter($filter['field'], $operation, $value);
			}
		}

		// populate data area info
		if ($aiGeneratedPivotStructure['dataCategory']) {
			$pivotDefObj->dataField = $aiGeneratedPivotStructure['dataCategory']['field'];
			$pivotDefObj->dataAggFunction = $aiGeneratedPivotStructure['dataCategory']['aggregateFunction'];
			if ($aiGeneratedPivotStructure['dataCategory']['currency']) {
				$pivotDefObj->dataCurrency = $aiGeneratedPivotStructure['dataCategory']['currency'];
			}
		}

		// populate rows area info
		if ($aiGeneratedPivotStructure['rowsCategory']) {
			$pivotDefObj->rowsField = $aiGeneratedPivotStructure['rowsCategory']['field'];
			$pivotDefObj->rowsDisplayOthers = $aiGeneratedPivotStructure['rowsCategory']['displayOthers'];
			$pivotDefObj->rowsLimit = $aiGeneratedPivotStructure['rowsCategory']['limit'];
			if ($aiGeneratedPivotStructure['rowsCategory']['orderField']=='currentField') { // current field
				$pivotDefObj->rowsOrderField = $aiGeneratedPivotStructure['rowsCategory']['field'];
			} else { // dataField
				$pivotDefObj->rowsOrderField = $aiGeneratedPivotStructure['dataCategory']['field'];
				$pivotDefObj->rowsOrderAggFunction = $aiGeneratedPivotStructure['dataCategory']['aggregateFunction'];
			}
			$pivotDefObj->rowsOrderDirection = $aiGeneratedPivotStructure['rowsCategory']['orderDirection'];
		}

		// populate cols area info
		if ($aiGeneratedPivotStructure['colsCategory']) {
			$pivotDefObj->colsField = $aiGeneratedPivotStructure['colsCategory']['field'];
			$pivotDefObj->colsDisplayOthers = $aiGeneratedPivotStructure['colsCategory']['displayOthers'];
			$pivotDefObj->colsLimit = $aiGeneratedPivotStructure['colsCategory']['limit'];
			if ($aiGeneratedPivotStructure['colsCategory']['orderField']=='currentField') { // current field
				$pivotDefObj->colsOrderField = $aiGeneratedPivotStructure['colsCategory']['field'];
			} else { // dataField
				$pivotDefObj->colsOrderField = $aiGeneratedPivotStructure['dataCategory']['field'];
				$pivotDefObj->colsOrderAggFunction = $aiGeneratedPivotStructure['dataCategory']['aggregateFunction'];
			}
			$pivotDefObj->colsOrderDirection = $aiGeneratedPivotStructure['colsCategory']['orderDirection'];
		}

	}

}