<?php

namespace Modules\Reports\AriaReports\Model\AnalyzeReport;

class Request extends \Modules\Reports\AriaReports\Model\RequestAbstract
{

	public string $aiModel = 'gpt-4.1-mini'; // gpt-5.1-2025-11-13, gpt-5-mini, gpt-4o-mini

	protected string $userPrompt = 'Analyze the following pivot table. Return a short interpretation.';

	protected array $pivotTableContent;

	/**
	 * Sets the pivot table content.
	 * @param array $pivotTableContent
	 */
	public function setPivotTableContent(array $pivotTableContent): void
	{
		$this->pivotTableContent = $pivotTableContent;
	}

	protected function init(): void
	{

/*
 * V1:
 * You have the role of a general manager of a travel agency (you are the CEO, COO and the CFO at the same time). You and helping me decide how to create a report from my application database. I will try to explain what kind of report I need but I have very limited experience with analysing data and reports.
 * I have a couple of specific database tables that I can use to extract my report. You can select only one of these table per report but, if my request is not clear enough, you will have to ask new helping questions to me or, at least, help me with report suggestions.
 * When you cannot decide on the chosen table and reply with a text message, do not include references to the internal names of the tables presented here, just suggest some report types or categories to help me decide what data I need. Try to be very concise and short when replying back.
 * If the user prompt is far away from a request that is not even remotely associated with the concept of a report, it means that the user is mocking you; in this case you can reply with a short and funny message but do not engage in the request.
 */
		$this->systemRole = trim('
You are helping me interpret the results of a pivot table. You may include political/geo-political or economical implications when analyzing the data but consider the interval that was used and the events that took place at that moment, if you think they are relevant for the analysis. 
Treat nulls as transactions that are missing for that certain cell - do not mention the word "null" in your output.
If I am analzing current year or previous year, your output should also help me decide what to do in the future based on previous data.
');
		// include the list of countries from offices list
		$offListObj = new \App\EntityList\Offices();
		$offListObj->setFilter('countryCode', '!=', '');
		$offListObj->setColumns(['countryCode']);
		$offList = $offListObj->exportRecordsByColumn('countryCode');
		if ($offList) {
			$this->systemRole .= "\n" . sprintf('These are the ISO country codes where my agency has offices: %s.', implode(', ', array_unique($offList))	);
		}

		$this->systemRole .= "\n" . 'Do not engage the user in next prompts.';

	}

	protected function populateBody(array &$body): void
	{

		if (!isset($this->pivotTableContent)) {
			throw new \RuntimeException('No pivot table content set.');
		}

		$body['input'][] = [
			'type' => 'message',
			'role'=>'user',
			'content' =>  json_encode($this->pivotTableContent),
		];

	}

}
