<?php
/**
 * Analytics Display
 */

if (!defined('ABSPATH')) {
    exit;
}

$analytics = new CCSPLLMS_Analytics();
$data = $analytics->get_analytics_data();
?>



<div class="ccspllms_container">
   
    <div class="ccspllms_header">
        <div class="ccspllmshead_left">
            <div class="ccspllmshead_logo">
            <img src="<?php echo esc_html( CCSPLLMS_PLUGIN_URL . 'assets/cloudcusplogo.svg' ); ?>" alt="Logo" class="ccspllms_logo">
            </div>
            <div class="ccspllmshead_text"><h1>LLMS.txt  Generator</h1></div>
        </div>
        <div class="ccspllmshead_right">
             <h1><i class="fas fa-chart-bar"></i> Bot Analytics</h1>
        <p>Monitor AI bot activity and file access</p>
        </div>
        
    </div>

    <div class="ccspllms_content">
        <div class="ccspllms_stats_grid">
            <div class="ccspllms_stat_card">
                <h3>Total Visits</h3>
                <div class="stat-number"><?php echo esc_html( $data['totals']['total_visits'] ?? 0 ); ?></div>
            </div>
            <div class="ccspllms_stat_card">
                <h3>Unique Visitors</h3>
                <div class="stat-number"><?php echo esc_html( $data['totals']['unique_visitors'] ?? 0 ); ?></div>
            </div>
            <div class="ccspllms_stat_card">
                <h3>Unique Bots</h3>
                <div class="stat-number"><?php echo esc_html( $data['totals']['unique_bots'] ?? 0 ); ?></div>
            </div>
            <div class="ccspllms_stat_card">
                <h3>Today's Visits</h3>
                <div class="stat-number"><?php echo esc_html( $data['totals']['today_visits'] ?? 0 ); ?></div>
            </div>
        </div>

        <div class="ccspllms_card">
            <h3>Daily Activity Chart</h3>
            <canvas id="ccspllms_analytics_chart" width="400" height="200"></canvas>
        </div>

        <div class="ccspllms_card">
            <h3>Top Bots</h3>
            <?php if (!empty($data['bot_stats'])): ?>
                <table class="ccspllms_table">
                    <thead>
                        <tr>
                            <th>Bot Name</th>
                            <th>Visit Count</th>
                            <th>Last Visit</th>
                            <th>Unique IPs</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($data['bot_stats'] as $bot): ?>
                            <tr>
                                <td><?php echo esc_html($bot->bot_name); ?></td>
                                <td><?php echo number_format($bot->visit_count); ?></td>
                                <td><?php echo esc_html(gmdate('M j, Y H:i', strtotime($bot->last_visit))); ?></td>
                                <td><?php echo esc_html($bot->unique_ips); ?></td>
                                <td>
                                    <label class="ccspllms_toggle">
                                        <input type="checkbox"
                                            class="bot_status_toggle"
                                            data-bot="<?php echo esc_attr($bot->bot_name); ?>"
                                            <?php echo  ($bot->status === 'allowed') ? 'checked' : ''; ?>>
                                        <span class="ccspllms_slider"></span>
                                    </label>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <p>No bot activity recorded yet.</p>
            <?php endif; ?>
        </div>

        <div class="ccspllms_card">
            <h3>Export Data</h3>
            <p>Download your analytics data for external analysis.</p>
            <button id="ccspllms_export_csv" class="ccspllms_button">
                <i class="fas fa-file-csv"></i> Export as CSV
            </button>
            <button id="ccspllms_export_json" class="ccspllms_button">
                <i class="fas fa-file-code"></i> Export as JSON
            </button>
        </div>

                


    </div>
</div>