<?php
defined('ABSPATH') || exit;

// Enqueue required scripts
wp_enqueue_script('ai-botkit-chartjs', AI_BOTKIT_PLUGIN_URL . 'admin/js/chart.js', array('jquery'), '4.4.1', true);
wp_enqueue_script('ai-botkit-chartjs-adapter', AI_BOTKIT_PLUGIN_URL . 'admin/js/chartjs-adapter-date-fns.js', array('ai-botkit-chartjs'), '3.0.0', true);

// nonce check
if (!isset($_GET['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash ( $_GET['nonce'] ) ), 'ai_botkit_chatbots' ) ) {
    wp_die(__('Invalid request', 'ai-botkit-for-lead-generation'));
}

use AI_BotKit\Integration\API_Handler;

$api_handler = new API_Handler();

// Get date range
$time_range = isset($_GET['time_range']) ? sanitize_text_field($_GET['time_range']) : '7';

$analytics_data = $api_handler->send_get_request('analytics', ['days' => $time_range]);

$graph_data = $api_handler->send_get_request('analytics/daily-usage', ['days' => $time_range]);

$total_chatbots = isset($analytics_data['totalChatbots']) ? $analytics_data['totalChatbots'] : 0;
$total_conversations = isset($analytics_data['totalConversations']) ? $analytics_data['totalConversations'] : 0;
$total_messages = isset($analytics_data['totalMessagesByUsers']) ? $analytics_data['totalMessagesByUsers'] : 0;
$total_tokens = isset($analytics_data['totalStorageCharacters']) ? $analytics_data['totalStorageCharacters'] : 0;


wp_localize_script('ai-botkit-chartjs', 'ai_botkitAnalytics', array(
    'timeSeriesData' => $graph_data,
    'i18n' => array(
        'totalConversations' => __('Total Conversations', 'ai-botkit-for-lead-generation'),
        'totalMessages' => __('Total Messages', 'ai-botkit-for-lead-generation'),
    )
));
?>

<div class="ai-botkit-knowledge-container">
    <div class="ai-botkit-knowledge-header">
		<div class="ai-botkit-knowledge-header-left">
            <h1 class="ai-botkit-knowledge-title"><?php esc_html_e('Analytics Dashboard', 'ai-botkit-for-lead-generation'); ?></h1>
            <p class="ai-botkit-knowledge-description"><?php esc_html_e('Track performance metrics for your chatbots', 'ai-botkit-for-lead-generation'); ?></p>
		</div>

		<div class="ai-botkit-knowledge-buttons">
            <form method="get" action="<?php echo esc_url(admin_url('admin.php')); ?>" id="ai_botkit_analytics_form">
                <input type="hidden" name="page" value="ai-botkit">
                <input type="hidden" name="tab" value="analytics">
                <input type="hidden" name="nonce" value="<?php echo esc_attr(wp_create_nonce('ai_botkit_chatbots')); ?>">
                <div class="ai-botkit-form-group">
                    <select name="time_range" id="ai_botkit_analytics_time_range">
                        <option value="7" <?php selected($time_range, '7'); ?>><?php esc_html_e('7 Days', 'ai-botkit-for-lead-generation'); ?></option>
                        <option value="30" <?php selected($time_range, '30'); ?>><?php esc_html_e('30 Days', 'ai-botkit-for-lead-generation'); ?></option>
                        <option value="90" <?php selected($time_range, '90'); ?>><?php esc_html_e('90 Days', 'ai-botkit-for-lead-generation'); ?></option>
                        <option value="365" <?php selected($time_range, '365'); ?>><?php esc_html_e('1 Year', 'ai-botkit-for-lead-generation'); ?></option>
                    </select>
                </div>
            </form>
		</div>
	</div>

    <div class="ai-botkit-knowledge-stats">
		<div class="ai-botkit-knowledge-stats-item">
			<div class="ai-botkit-stats-body">
				<span class="ai-botkit-knowledge-stats-item-label"><?php esc_html_e('Total Chatbots', 'ai-botkit-for-lead-generation'); ?></span>
				<span class="ai-botkit-knowledge-stats-item-value"><?php echo esc_html(number_format_i18n($total_chatbots)); ?></span>
			</div>
		</div>
		<div class="ai-botkit-knowledge-stats-item">
			<div class="ai-botkit-stats-body">
				<span class="ai-botkit-knowledge-stats-item-label"><?php esc_html_e('Total Conversations', 'ai-botkit-for-lead-generation'); ?></span>
				<span class="ai-botkit-knowledge-stats-item-value"><?php echo esc_html(number_format_i18n($total_conversations)); ?></span>
			</div>
		</div>
		<div class="ai-botkit-knowledge-stats-item">
			<div class="ai-botkit-stats-body">
				<span class="ai-botkit-knowledge-stats-item-label"><?php esc_html_e('Total Messages', 'ai-botkit-for-lead-generation'); ?></span>
				<span class="ai-botkit-knowledge-stats-item-value"><?php echo esc_html(number_format_i18n($total_messages)); ?></span>
			</div>
		</div>
		<div class="ai-botkit-knowledge-stats-item">
			<div class="ai-botkit-stats-body">
				<span class="ai-botkit-knowledge-stats-item-label"><?php esc_html_e('Knowledge Base Chars', 'ai-botkit-for-lead-generation'); ?></span>
				<span class="ai-botkit-knowledge-stats-item-value"><?php echo esc_html(number_format_i18n($total_tokens)); ?></span>
			</div>
		</div>
	</div>

    <div class="ai-botkit-charts-grid">
        <div class="ai-botkit-chart-card">
            <h3><?php esc_html_e('Daily Activity Overview', 'ai-botkit-for-lead-generation'); ?></h3>
            <canvas id="usageChart"></canvas>
        </div>
    </div>
</div>
