<?php

declare( strict_types=1 );

namespace Limb_Chatbot\Includes\Services\System_Message_Generators;

use Limb_Chatbot\Includes\Data_Objects\Chatbot;
use Limb_Chatbot\Includes\Interfaces\System_Message_Generator_Interface;
use Limb_Chatbot\Includes\Services\Chatbot_Profile_Manager;
use Limb_Chatbot\Includes\Services\System_Message\Chunks\Actions_Chunk;
use Limb_Chatbot\Includes\Services\System_Message\Chunks\Custom_Instructions_Chunk;
use Limb_Chatbot\Includes\Services\System_Message\Chunks\User_Information_Chunk;
use Limb_Chatbot\Includes\Services\System_Message\Chunks\Verified_Knowledge_Chunk;
use Limb_Chatbot\Includes\Services\System_Message\System_Message_Assembler;
use Limb_Chatbot\Includes\Utilities\Chatbot_Utility;

/**
 * Adaptive Universal System Message Generator
 *
 * Uses the System_Message_Assembler to build modular system messages from chunks.
 *
 * Characteristics:
 * - Chunk-based architecture for flexibility
 * - No response modes or length flags
 * - Natural, intent-driven verbosity
 * - Reduced rule conflicts
 * - Faster and more predictable responses
 * - Knowledge-faithful and hallucination-resistant
 *
 * @since 1.1.1
 */
class Chatbot_System_Message implements System_Message_Generator_Interface {

	protected Chatbot_Profile_Manager $profile_manager;

	public function __construct( Chatbot $chatbot ) {
		$this->profile_manager = new Chatbot_Profile_Manager( $chatbot );
	}

	public function generate( Chatbot_Utility $chatbot_utility, array $knowledge = [] ): string {
		// Create assembler with base chunks
		$assembler = new System_Message_Assembler();

		// Add chunks in proper priority order
		$assembler->add_chunk( new Custom_Instructions_Chunk( $chatbot_utility ) );
		// Other dynamic chunks
		$assembler->add_chunk( new User_Information_Chunk( $chatbot_utility ) );
		$assembler->add_chunk( new Verified_Knowledge_Chunk( $knowledge ) );
		$assembler->add_chunk( new Actions_Chunk( $chatbot_utility ) );


		return $assembler->build();
	}
}