<?php

namespace Limb_Chatbot\Includes\Data_Objects;

use Limb_Chatbot\Includes\Agent_Operations\Agent_Operation;
use Limb_Chatbot\Includes\Agent_Operations\Operation;
use Limb_Chatbot\Includes\Services\Helper;

/**
 * Agent data object.
 *
 * Represents a domain-specific agent that can be switched to during a conversation.
 *
 * @package Limb_Chatbot\Includes\Data_Objects
 * @since 1.0.0
 */
class Agent {

	/**
	 * Agent identifier.
	 *
	 * @var string
	 * @since 1.0.0
	 */
	public string $id;

	/**
	 * Agent display name.
	 *
	 * @var string
	 * @since 1.0.0
	 */
	public string $name;
	/**
	 * Agent description.
	 *
	 * @var string
	 * @since 1.0.0
	 */
	public string $description;

	/**
	 * Agent operations.
	 *
	 * @var array|null
	 * @since 1.0.0
	 */
	public ?array $operations = [];

	/**
	 * System message generator class name.
	 *
	 * @var string
	 * @since 1.0.0
	 */
	private string $system_message_generator = '';

	/**
	 * Constructor.
	 * @since 1.0.0
	 */
	public function __construct() {
	}

	/**
	 * Get agent identifier.
	 *
	 * @return string Agent ID.
	 * @since 1.0.0
	 */
	public function get_id(): string {
		return $this->id;
	}

	public function set_id( string $id ): void {
		$this->id = $id;
	}

	/**
	 * Get agent display name.
	 *
	 * @return string Agent name.
	 * @since 1.0.0
	 */
	public function get_name(): string {
		return $this->name;
	}

	public function set_name( string $name ): void {
		$this->name = $name;
	}

	/**
	 * Get agent description.
	 *
	 * @return string Agent description.
	 * @since 1.0.0
	 */
	public function get_description(): string {
		return $this->description;
	}

	public function set_description( string $description ): void {
		$this->description = $description;
	}


	/**
	 * Convert agent to array.
	 *
	 * @return array Agent data as array.
	 * @since 1.0.0
	 */
	public function to_array(): array {
		return [
			'id'          => $this->id,
			'name'        => $this->name,
			'description' => $this->description,
			'operations'  => $this->operations,
		];
	}

	public function get_operations(): ?array {
		return $this->operations;
	}

	public function set_operations( ?array $operations ): void {
		$this->operations = $operations;
	}

	public function add_operation( ?string $operation_name, $integration = null ): void {
		$class_name     = "Limb_Chatbot\\Includes";
		$operation_name = Helper::underscore_to_upper( $operation_name );
		if ( $integration ) {
			$class_name .= "\\Integrations\\$integration\\Operations\\$operation_name";
		} else {
			$class_name .= "\\Operations\\$operation_name";
		}

		if ( class_exists( $class_name ) ) {
			$operation = new $class_name();
			if ( $operation instanceof Agent_Operation ) {
				if ( ! is_array( $this->operations ) ) {
					$this->operations = [];
				}
				$this->operations[] = $operation;
			}
		}
	}

	/**
	 * Get system message generator class name.
	 *
	 * @return string System message generator class name.
	 * @since 1.0.0
	 */
	public function get_system_message_generator(): string {
		return $this->system_message_generator;
	}

	/**
	 * Set system message generator class name.
	 *
	 * @param string $class_name System message generator class name.
	 * @return void
	 * @since 1.0.0
	 */
	public function set_system_message_generator( string $class_name ): void {
		$this->system_message_generator = $class_name;
	}
}
