<?php
/**
 * Stablezact Admin Analytics Page Template
 */

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

// Check if API is configured (using public_key authentication)
$stablezact_is_configured = !empty($this->gateway_settings['public_key']);
?>

<div class="wrap stablezact-admin">
    <div class="stablezact-header">
        <div class="stablezact-header-content">
            <h1><?php esc_html_e('Analytics & Reports', 'stablezact-crypto-payments'); ?></h1>
            <p><?php esc_html_e('View detailed analytics and insights about your cryptocurrency payments', 'stablezact-crypto-payments'); ?></p>
        </div>
        <?php if ($stablezact_is_configured): ?>
            <div class="stablezact-header-actions">
                <button class="button button-primary" id="refresh-analytics">
                    <span class="dashicons dashicons-update"></span>
                    <?php esc_html_e('Refresh Data', 'stablezact-crypto-payments'); ?>
                </button>
            </div>
        <?php endif; ?>
    </div>

    <?php if (!$stablezact_is_configured): ?>
        <div class="notice notice-warning">
            <p>
                <strong><?php esc_html_e('Setup Required', 'stablezact-crypto-payments'); ?></strong><br>
                <?php
                printf(
                    /* translators: %s: link to settings page */
                    esc_html__('Please configure your Stablezact API credentials in the %s to view analytics.', 'stablezact-crypto-payments'),
                    '<a href="' . esc_url( admin_url('admin.php?page=stablezact-settings') ) . '">' . esc_html__('settings page', 'stablezact-crypto-payments') . '</a>'
                );
                ?>
            </p>
        </div>
    <?php else: ?>

        <!-- Overview Stats -->
        <?php if ($stats): ?>
        <div class="stablezact-analytics-overview">
            <div class="analytics-stats-grid">
                <div class="analytics-stat-card">
                    <div class="stat-header">
                        <h3><?php esc_html_e('Total Volume', 'stablezact-crypto-payments'); ?></h3>
                        <span class="stat-period"><?php esc_html_e('All Time', 'stablezact-crypto-payments'); ?></span>
                    </div>
                    <div class="stat-value">
                        $<?php echo esc_html(number_format($stats['totalVolume'] ?? 0, 2)); ?>
                    </div>
                    <div class="stat-change positive">
                        <span class="dashicons dashicons-arrow-up-alt"></span>
                        <?php esc_html_e('Total earnings', 'stablezact-crypto-payments'); ?>
                    </div>
                </div>

                <div class="analytics-stat-card">
                    <div class="stat-header">
                        <h3><?php esc_html_e('Success Rate', 'stablezact-crypto-payments'); ?></h3>
                        <span class="stat-period"><?php esc_html_e('All Time', 'stablezact-crypto-payments'); ?></span>
                    </div>
                    <div class="stat-value">
                        <?php
                        $stablezact_successRate = $stats['totalPayments'] > 0
                            ? round(($stats['completedPayments'] / $stats['totalPayments']) * 100, 1)
                            : 0;
                        echo esc_html($stablezact_successRate . '%');
                        ?>
                    </div>
                    <div class="stat-change <?php echo esc_attr($stablezact_successRate >= 90 ? 'positive' : 'neutral'); ?>">
                        <span class="dashicons dashicons-chart-line"></span>
                        <?php echo esc_html($stats['completedPayments']); ?>/<?php echo esc_html($stats['totalPayments']); ?> <?php esc_html_e('completed', 'stablezact-crypto-payments'); ?>
                    </div>
                </div>
            </div>
        </div>
        <?php endif; ?>

    <?php endif; ?>
</div>
