<?php
/**
 * Admin Pages class for managing plugin admin pages.
 *
 * This class handles the creation and management of all admin pages,
 * including settings, Q&A management, and content indexing.
 *
 * @package Askany
 * @since 1.0.0
 */

namespace Askany;

use ask\askany\wpnts_nts_fs;

/**
 * Admin Pages class for managing plugin admin pages.
 *
 * @package Askany
 * @since 1.0.0
 */
class AdminPages {
	/**
	 * API Manager instance for handling AI interactions.
	 *
	 * @var ApiManager
	 */
	private $api_manager;
	/**
	 * Database instance for managing plugin data.
	 *
	 * @var Database
	 */
	private $database;
	/**
	 * Content Indexer instance for managing content indexing.
	 *
	 * @var ContentIndexer
	 */
	private $content_indexer;
	/**
	 * PDF Handler instance for managing PDF files.
	 *
	 * @var PdfHandler
	 */
	private $pdf_handler;

	/**
	 * External Sources Handler instance for managing external sources.
	 *
	 * @var ExternalSourcesHandler
	 */
	private $external_sources_handler;

	/**
	 * Constructor for AdminPages.
	 *
	 * Initializes the API Manager, Database, Content Indexer, PDF Handler, and External Sources Handler instances.
	 *
	 * @param ApiManager              $api_manager              API Manager instance.
	 * @param Database                $database                 Database instance.
	 * @param ContentIndexer          $content_indexer          Content Indexer instance.
	 * @param PdfHandler              $pdf_handler              PDF Handler instance.
	 * @param ExternalSourcesHandler  $external_sources_handler External Sources Handler instance.
	 */
	public function __construct( $api_manager, $database, $content_indexer, $pdf_handler, $external_sources_handler ) {
		$this->api_manager = $api_manager;
		$this->database = $database;
		$this->content_indexer = $content_indexer;
		$this->pdf_handler = $pdf_handler;
		$this->external_sources_handler = $external_sources_handler;
	}

	/**
	 * Initialize the AdminPages class.
	 *
	 * This method hooks into WordPress to add admin menu items and enqueue scripts/styles.
	 *
	 * @return void
	 */
	public function init() {
		add_action('admin_menu', array( $this, 'add_admin_menu' ));
		add_action('admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ));
		add_action('admin_enqueue_scripts', array( $this, 'enqueue_global_admin_assets' ));
	}

	/**
	 * Add admin menu items.
	 *
	 * This method adds the main menu item and all submenu items for the plugin.
	 *
	 * @return void
	 */
	public function add_admin_menu() {
		// Add main menu item.
		add_menu_page(
			__('Askany AI', 'askany'),
			__('Askany AI', 'askany'),
			'manage_options',
			'askany-settings',
			array( $this, 'render_settings_page' ),
			'dashicons-format-chat',
			30
		);

		// Add submenu items.
		add_submenu_page(
			'askany-settings',
			__('Settings', 'askany'),
			__('Settings', 'askany'),
			'manage_options',
			'askany-settings',
			array( $this, 'render_settings_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('Content Index', 'askany'),
			__('Content Index', 'askany'),
			'manage_options',
			'askany-content-index',
			array( $this, 'render_content_index_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('PDF Files', 'askany'),
			__('PDF Files', 'askany'),
			'manage_options',
			'askany-pdf-files',
			array( $this, 'render_pdf_files_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('External Sources', 'askany'),
			__('External Sources', 'askany'),
			'manage_options',
			'askany-external-sources',
			array( $this, 'render_external_sources_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('RAG Management', 'askany'),
			__('RAG Management', 'askany'),
			'manage_options',
			'askany-rag-management',
			array( $this, 'render_rag_management_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('Q&A Management', 'askany'),
			__('Q&A Management', 'askany'),
			'manage_options',
			'askany-qa-management',
			array( $this, 'render_qa_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('Home Page Settings', 'askany'),
			__('Home Page Settings', 'askany'),
			'manage_options',
			'askany-home-settings',
			array( $this, 'render_home_settings_page' )
		);

		// Get unread message count for menu badge
		$unread_count = $this->database->get_total_unread_message_count( 'user' );
		$menu_title = __('Live Chat', 'askany');
		if ( $unread_count > 0 ) {
			$menu_title .= ' <span class="askany-unread-badge">' . (int) $unread_count . '</span>';
		}

		add_submenu_page(
			'askany-settings',
			__('Live Chat', 'askany'),
			$menu_title,
			'manage_options',
			'askany-live-chat',
			array( $this, 'render_live_chat_page' )
		);

		// Integration menu
		add_submenu_page(
			'askany-settings',
			__('Integration', 'askany'),
			__('Integration', 'askany'),
			'manage_options',
			'askany-integration',
			array( $this, 'render_integration_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('User List & Agents', 'askany'),
			__('User List & Agents', 'askany'),
			'manage_options',
			'askany-user-agents',
			array( $this, 'render_user_agents_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('Database Management', 'askany'),
			__('Database Management', 'askany'),
			'manage_options',
			'askany-database-management',
			array( $this, 'render_database_management_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('Chat Logs', 'askany'),
			__('Chat Logs', 'askany'),
			'manage_options',
			'askany-chat-logs',
			array( $this, 'render_chat_logs_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('API Statistics', 'askany'),
			__('API Statistics', 'askany'),
			'manage_options',
			'askany-api-stats',
			array( $this, 'render_api_stats_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('Feature Requests', 'askany'),
			__('Feature Requests', 'askany'),
			'manage_options',
			'askany-feature-requests',
			array( $this, 'render_feature_requests_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('Bug Reports', 'askany'),
			__('Bug Reports', 'askany'),
			'manage_options',
			'askany-bug-reports',
			array( $this, 'render_bug_reports_page' )
		);

		add_submenu_page(
			'askany-settings',
			__('Recommended Plugins', 'askany'),
			__('Recommended Plugins', 'askany'),
			'manage_options',
			'askany-recommendations',
			array( $this, 'render_recommendations_page' )
		);
	}

	
	public function enqueue_admin_assets( $hook ) {
		// Only load on our plugin pages.
		if ( strpos($hook, 'askany') === false ) {
			return;
		}

		wp_enqueue_media();
		wp_enqueue_script('media-upload');
		wp_enqueue_script('thickbox');
		wp_enqueue_style('thickbox');

		wp_enqueue_style(
			'askany-admin',
			ASKANY_PLUGIN_URL . 'dist/css/admin.min.css',
			array(),
			ASKANY_VERSION
		);

		// Enqueue Pro features CSS
		wp_enqueue_style(
			'askany-pro-features',
			ASKANY_PLUGIN_URL . 'dist/css/pro-features.min.css',
			array(),
			ASKANY_VERSION
		);
		
		// Enqueue Dynamic Pricing CSS
		wp_enqueue_style(
			'askany-dynamic-pricing',
			ASKANY_PLUGIN_URL . 'dist/css/dynamic-pricing.min.css',
			array(),
			ASKANY_VERSION
		);

		wp_enqueue_script(
			'askany-toastmaster',
			ASKANY_PLUGIN_URL . 'assets/library/toastmaster.min.js',
			array(),
			ASKANY_VERSION,
			true
		);
		wp_enqueue_script(
			'askany-chart',
			ASKANY_PLUGIN_URL . 'assets/library/chart.js',
			array(),
			ASKANY_VERSION,
			true
		);
		
		// Enqueue Dynamic Pricing JS (before Pro modal)
		wp_enqueue_script(
			'askany-dynamic-pricing',
			ASKANY_PLUGIN_URL . 'dist/js/dynamic-pricing.min.js',
			array( 'jquery' ),
			ASKANY_VERSION,
			true
		);
		
		// Enqueue Pro modal JS (depends on dynamic pricing)
		wp_enqueue_script(
			'askany-pro-modal',
			ASKANY_PLUGIN_URL . 'dist/js/pro-modal.min.js',
			array( 'jquery', 'askany-dynamic-pricing' ),
			ASKANY_VERSION,
			true
		);
		
		wp_enqueue_script(
			'askany-admin',
			ASKANY_PLUGIN_URL . 'dist/js/admin.min.js',
			array( 'jquery', 'askany-pro-modal' ),
			ASKANY_VERSION,
			true
		);

		// Enqueue RAG processor script
		wp_enqueue_script(
			'askany-rag-processor',
			ASKANY_PLUGIN_URL . 'dist/js/rag-processor.min.js',
			array( 'jquery', 'askany-admin' ),
			ASKANY_VERSION,
			true
		);

		// Enqueue Markdown Parser (dependency for markdown editor)
		wp_enqueue_script(
			'askany-markdown-parser',
			ASKANY_PLUGIN_URL . 'dist/js/markdown-parser.min.js',
			array(),
			ASKANY_VERSION,
			true
		);

		// Enqueue Markdown Editor
		wp_enqueue_style(
			'askany-markdown-editor',
			ASKANY_PLUGIN_URL . 'dist/css/markdown-editor.min.css',
			array(),
			ASKANY_VERSION
		);
		wp_enqueue_script(
			'askany-markdown-editor',
			ASKANY_PLUGIN_URL . 'dist/js/markdown-editor.min.js',
			array( 'jquery', 'askany-markdown-parser' ),
			ASKANY_VERSION,
			true
		);

		wp_localize_script('askany-admin', 'askanyAdmin', array(
			'ajaxUrl' => admin_url('admin-ajax.php'),
			'nonce' => wp_create_nonce('askany_chat_nonce'),
			'restUrl' => rest_url(),
			'restNonce' => wp_create_nonce('wp_rest'),
			'isPro' => \Askany\Plugin::get_instance()->is_pro_active(),
			'mediaUploader' => array(
				'title' => __('Select Assistant Avatar', 'askany'),
				'button' => array(
					'text' => __('Use this image', 'askany'),
				),
			),
			'i18n' => array(
				'confirmReindex' => __('Are you sure you want to reindex all content? This may take some time.', 'askany'),
				'reindexSuccess' => __('Content reindexing started successfully.', 'askany'),
				'reindexError' => __('Error starting content reindexing.', 'askany'),
				'saveSuccess' => __('Settings saved successfully.', 'askany'),
				'saveError' => __('Error saving settings.', 'askany'),
				'loading' => __('Loading...', 'askany'),
				'testConnection' => __('Test Connection', 'askany'),
				'noApiKey' => __('Please enter an API key first', 'askany'),
				'uploadSuccess' => __('PDF uploaded successfully.', 'askany'),
				'qaupdateSuccess' => __('QA updated successfully.', 'askany'),
				'qaaddSuccess' => __('QA added successfully.', 'askany'),
				'uploadError' => __('Error uploading PDF.', 'askany'),
				'mediaError' => __('WordPress media uploader is not available. Please refresh the page and try again.', 'askany'),
			),
		));
	}

	/**
	 * Enqueue admin assets.
	 *
	 * This method enqueues the necessary scripts and styles for the admin pages.
	 *
	 * @param string $hook The current admin page hook.
	 * @return void
	 */
	public function enqueue_global_admin_assets( $hook ) {
		wp_enqueue_style(
			'askany-admin',
			ASKANY_PLUGIN_URL . 'dist/css/global.min.css',
			array(),
			ASKANY_VERSION
		);
	}

	/**
	 * Render the settings page.
	 *
	 * This method renders the main settings page for the plugin.
	 *
	 * @return void
	 */
	public function render_settings_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Get data to pass to the template.
		$data = array(
			'settings' => $this->api_manager->get_settings(),
			'content_stats' => $this->content_indexer->get_content_stats(),
			'pdf_stats' => $this->pdf_handler->get_pdf_stats(),
		);

		// Include the template file.
		$this->load_template('admin-settings.php', $data);
	}

	/**
	 * Render the content index page.
	 *
	 * This method renders the content index page for the plugin.
	 *
	 * @return void
	 */
	public function render_content_index_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Get data to pass to the template.
		$data = array(
			'content_stats' => $this->content_indexer->get_content_stats(),
		);

		// Include the template file.
		$this->load_template('admin-content-index.php', $data);
	}

	/**
	 * Render the PDF files page.
	 *
	 * This method renders the PDF files page for the plugin.
	 *
	 * @return void
	 */
	public function render_pdf_files_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Get data to pass to the template.
		$data = array(
			'pdf_stats' => $this->pdf_handler->get_pdf_stats(),
		);

		// Include the template file.
		$this->load_template('admin-pdf-import.php', $data);
	}

	/**
	 * Render the chat logs page.
	 *
	 * This method renders the chat logs page for the plugin.
	 *
	 * @return void
	 */
	public function render_chat_logs_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Get data to pass to the template.
		$data = array(
			'chat_history' => $this->database->get_chat_history(100),
		);

		// Include the template file.
		$this->load_template('admin-chat-logs.php', $data);
	}

	/**
	 * Render the Q&A management page.
	 *
	 * This method renders the Q&A management page for the plugin.
	 *
	 * @return void
	 */
	public function render_qa_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Get data to pass to the template.
		$data = array(
			'qa_pairs' => $this->database->get_qa_pairs(),
		);

		// Include the template file.
		$this->load_template('admin-qa.php', $data);
	}

	/**
	 * Render the live chat management page.
	 *
	 * This method renders the live chat management page for the plugin.
	 *
	 * @return void
	 */
	public function render_live_chat_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Check if pro version is active
		$plugin = \Askany\Plugin::get_instance();
		$is_pro = $plugin->is_pro_active();

		// Get data to pass to the template.
		$data = array(
			'chat_sessions' => $this->database->get_active_chat_sessions(),
			'agents' => $this->database->get_agents(),
			'current_user_id' => get_current_user_id(),
			'stats' => $this->database->get_session_statistics(),
			'is_pro' => $is_pro,
			'database' => $this->database, // Pass database instance for unread counts
		);

		// Include the template file.
		$this->load_template('admin-live-chat.php', $data);
	}

	/**
	 * Render the integration page.
	 *
	 * This method renders the integration management page for the plugin.
	 *
	 * @return void
	 */
	public function render_integration_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Check if we're viewing a specific integration (like Slack)
		$integration = isset( $_GET['integration'] ) ? sanitize_text_field( wp_unslash( $_GET['integration'] ) ) : '';
		
		if ( $integration === 'slack' ) {
			// Render Slack integration page
			$this->render_slack_integration_page();
			return;
		}

		// Check if pro version is active
		$plugin = \Askany\Plugin::get_instance();
		$is_pro = $plugin->is_pro_active();

		// Get integration settings
		$settings = $this->api_manager->get_settings();
		
		// Ensure settings is an array
		if ( ! is_array( $settings ) ) {
			$settings = array();
		}

		// Add WooCommerce settings (enabled by default)
		$settings['woocommerce_enabled'] = get_option( 'askany_woocommerce_enabled', true );
		$settings['woocommerce_order_tracking'] = get_option( 'askany_woocommerce_order_tracking', false );

		// Get data to pass to the template
		$data = array(
			'settings' => $settings,
			'is_pro' => $is_pro,
		);

		// Include the template file
		$this->load_template('admin-integration.php', $data);
	}

	/**
	 * Render the Slack integration page.
	 *
	 * This method renders the Slack integration setup page for the plugin.
	 *
	 * @return void
	 */
	public function render_slack_integration_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Check if pro version is active
		$plugin = \Askany\Plugin::get_instance();
		$is_pro = $plugin->is_pro_active();

		// Get Slack settings
		$settings = $this->api_manager->get_settings();
		
		// Ensure settings is an array
		if ( ! is_array( $settings ) ) {
			$settings = array();
		}
		
		$slack_settings = isset($settings['slack']) && is_array($settings['slack']) ? $settings['slack'] : array();

		// Get data to pass to the template
		$data = array(
			'settings' => $settings,
			'slack_settings' => $slack_settings,
			'is_pro' => $is_pro,
		);

		// Include the template file
		$this->load_template('admin-slack-integration.php', $data);
	}

	/**
	 * Render the user agents management page.
	 *
	 * This method renders the user agents management page for the plugin.
	 *
	 * @return void
	 */
	public function render_user_agents_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Get data to pass to the template.
		$data = array(
			'users' => $this->database->get_all_users(),
			'agents' => $this->database->get_agents(),
		);

		// Include the template file.
		$this->load_template('admin-user-agents.php', $data);
	}

	/**
	 * Render the database management page.
	 *
	 * This method renders the database management page for the plugin.
	 *
	 * @return void
	 */
	public function render_database_management_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Initialize database manager
		$database_manager = new \Askany\DatabaseManager( $this->database );

		// Enqueue styles and scripts for database management
		wp_enqueue_style( 'askany-database-management', ASKANY_PLUGIN_URL . 'dist/css/database-management.min.css', array(), ASKANY_VERSION );
		wp_enqueue_script( 'askany-database-management', ASKANY_PLUGIN_URL . 'dist/js/database-management.min.js', array( 'jquery' ), ASKANY_VERSION, true );

		// Check PRO status
		$plugin = \Askany\Plugin::get_instance();
		$is_pro = $plugin->is_pro_active();

		// Localize script
		wp_localize_script( 'askany-database-management', 'askanyDbMgmt', array(
			'nonce' => wp_create_nonce( 'askany_db_management' ),
			'isPro' => $is_pro,
			'strings' => array(
				'clean10Days' => __( 'Clean Last 10 Days', 'askany' ),
				'clean10DaysConfirm' => __( 'This will delete all chat data older than 10 days. This action cannot be undone.', 'askany' ),
				'clean30Days' => __( 'Clean Last 30 Days', 'askany' ),
				'clean30DaysConfirm' => __( 'This will delete all chat data older than 30 days. This action cannot be undone.', 'askany' ),
				'truncateAll' => __( 'Truncate All Data', 'askany' ),
				'truncateAllConfirm' => __( 'This will permanently delete ALL chat data including sessions, messages, and logs. This action cannot be undone!', 'askany' ),
				'optimizeTables' => __( 'Optimize Database Tables', 'askany' ),
				'optimizeTablesConfirm' => __( 'This will optimize all AskAny database tables to improve performance.', 'askany' ),
				'deleteFiltered' => __( 'Delete Filtered Data', 'askany' ),
				'deleteFilteredConfirm' => __( 'This will delete all chat data between the selected dates. This action cannot be undone.', 'askany' ),
				'selectDates' => __( 'Please select both start and end dates for filtering.', 'askany' ),
				'preparingExport' => __( 'Preparing export...', 'askany' ),
				'exportCompleted' => __( 'Export completed successfully!', 'askany' ),
				'exportFailed' => __( 'Export failed', 'askany' ),
				'importingData' => __( 'Importing data...', 'askany' ),
				'importCompleted' => __( 'Import completed successfully!', 'askany' ),
				'importFailed' => __( 'Import failed', 'askany' ),
				'applyingFilters' => __( 'Applying filters...', 'askany' ),
				'found' => __( 'Found', 'askany' ),
				'sessionsAnd' => __( 'sessions and', 'askany' ),
				'messagesInRange' => __( 'messages in the selected range.', 'askany' ),
				'filterFailed' => __( 'Filter failed', 'askany' ),
				'deleteFailed' => __( 'Delete operation failed', 'askany' ),
				'operationFailed' => __( 'Operation failed', 'askany' ),
				'networkError' => __( 'Network error occurred', 'askany' ),
				'networkErrorExport' => __( 'Network error occurred during export', 'askany' ),
				'networkErrorImport' => __( 'Network error occurred during import', 'askany' ),
				'upgradeToProTitle' => __( 'Upgrade to Pro', 'askany' ),
				'upgradeToProDesc' => __( 'Unlock advanced database management features', 'askany' ),
				'proFeature1' => __( 'Advanced database operations', 'askany' ),
				'proFeature2' => __( 'Data export & import', 'askany' ),
				'proFeature3' => __( 'Custom date filtering', 'askany' ),
				'proFeature4' => __( 'Bulk data cleanup', 'askany' ),
				'proFeature5' => __( 'Priority support', 'askany' ),
				'proRequired' => __( 'This feature requires AskAny Pro. Please upgrade to access advanced database management.', 'askany' ),
			)
		) );

		// Get database statistics
		$table_sizes = $database_manager->get_table_sizes();
		$record_counts = $database_manager->get_record_counts();
		$growth_analytics = $database_manager->get_growth_analytics( 30 );

		// Get data to pass to the template
		$data = array(
			'table_sizes' => $table_sizes,
			'record_counts' => $record_counts,
			'growth_analytics' => $growth_analytics,
			'database_manager' => $database_manager,
		);

		// Include the template file
		$this->load_template('admin-database-management.php', $data);
	}

	/**
	 * Render the home page settings management page.
	 *
	 * This method renders the home page settings management page for the plugin.
	 *
	 * @return void
	 */
	public function render_home_settings_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Get data to pass to the template.
		$data = array(
			'settings' => $this->api_manager->get_settings(),
			'faqs' => $this->database->get_all_home_faqs(),
			'custom_links' => $this->database->get_all_home_links(),
			'agents' => $this->database->get_agents(),
		);

		// Include the template file.
		$this->load_template('admin-home-settings.php', $data);
	}

	/**
	 * Render the API statistics page.
	 *
	 * This method renders the API statistics page with charts and performance metrics.
	 *
	 * @return void
	 */
	public function render_api_stats_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Get data to pass to the template.
		$data = array(
			'stats_30_days' => $this->database->get_api_stats(30),
			'stats_7_days' => $this->database->get_api_stats(7),
			'performance_today' => $this->database->get_api_performance_metrics('today'),
			'performance_week' => $this->database->get_api_performance_metrics('week'),
			'performance_month' => $this->database->get_api_performance_metrics('month'),
		);

		// Include the template file.
		$this->load_template('admin-api-stats.php', $data);
	}

	/**
	 * Render the external sources page.
	 *
	 * This method renders the external sources management page for the plugin.
	 *
	 * @return void
	 */
	public function render_external_sources_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Get data to pass to the template.
		$data = array(
			'stats' => $this->external_sources_handler->get_stats(),
		);

		// Include the template file.
		$this->load_template('admin-external-sources.php', $data);
	}

	/**
	 * Render RAG Management page.
	 *
	 * @return void
	 */
	public function render_rag_management_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Include the template file.
		$this->load_template('admin-rag-management.php');
	}

	/**
	 * Render recommendations page.
	 *
	 * @since 1.0.0
	 */
	public function render_recommendations_page() {
		if ( ! current_user_can('manage_options') ) {
			return;
		}

		// Include the template file.
		$this->load_template('admin-recommendations.php');
	}

	/**
	 * Render Feature Requests page.
	 *
	 * @return void
	 */
	public function render_feature_requests_page() {
		$this->load_template('admin-feature-requests.php');
	}

	/**
	 * Render Bug Reports page.
	 *
	 * @return void
	 */
	public function render_bug_reports_page() {
		$this->load_template('admin-bug-reports.php');
	}



	/**
	 * Loads and includes a template file with provided data.
	 *
	 * @param string $template_file The template file name to load.
	 * @param array  $data          Data to be available in the template scope.
	 * @return void
	 */
	private function load_template( $template_file, $data = array() ) {
		$template_path = ASKANY_PLUGIN_DIR . 'templates/' . $template_file;

		if ( ! file_exists( $template_path ) ) {
			// translators: %s: The name of the template file that couldn't be found
			$error_message = sprintf(
				__( 'Error: Template file %s not found.', 'askany' ),
				esc_html( $template_file )
			);
			wp_die( wp_kses_post( $error_message ) );
		}

		// Extract data variables to be accessible in the template.
		extract($data);

		// Include the template file.
		include $template_path;
	}
}
