<?php

namespace Modules\Reports\AriaReports\Model;

abstract class RequestAbstract
{

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

	protected string $userPrompt;

	protected string $systemRole;

	protected array $prevChatMessages = [];

	public function __construct(string $userPrompt)
	{
		$this->userPrompt = $userPrompt;
		$this->init();
	}

	public function setPrevChatMessages(array $prevChatMessages): void
	{
		$this->prevChatMessages = $prevChatMessages;
	}

	abstract protected function init(): void;

	public function getBody(): array
	{

		// cheks general inputs
		if (!$this->userPrompt) {
			throw new \RuntimeException('No input provided.');
		}
		if (!isset($this->systemRole)) {
			throw new \Exception('No system role provided.');
		}

		// add system role
		$body = [
			'model' => $this->aiModel,
			'input' => [
				[
					'type' => 'message',
					'role'=>'system',
					'content' =>  $this->systemRole,
				],
			],
		];

		// add prev chat messages
		foreach($this->prevChatMessages as $message) {
			$body['input'][] = [
				'type' => 'message',
				'role' => 'user',
				'content' => $message['userPrompt'],
			];
			if ($message['textReply']) {
				$body['input'][] = [
					'type' => 'message',
					'role' => 'assistant',
					'content' => $message['textReply'],
				];
			}
		}

		// add current user prompt
		$body['input'][] = [
			'type' => 'message',
			'role'=>'user',
			'content' =>  $this->userPrompt,
		];

		// populate specific content of the request
		$this->populateBody($body);

		return $body;

	}

	abstract protected function populateBody(array &$body): void;

}