<?php
/**
 * Visitors view: hourly chart + recent visits table + browser/OS breakdown.
 *
 * @package Brainwerk SEO Suite
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$scope   = Seoforge_Utils::stats_scope();
$hourly    = Seoforge_Stats::visits_hourly( 24, $scope );
$browser   = Seoforge_Stats::browser_breakdown( 24 * 7, $scope );
$os        = Seoforge_Stats::os_breakdown( 24 * 7, $scope );
$countries = Seoforge_Stats::geo_countries( 24 * 7, 250, $scope );
$recent    = Seoforge_Stats::recent_visits( 50, $scope );
$geo_pro   = class_exists( 'Seoforge_License' ) && Seoforge_License::is_pro();
?>
<?php Seoforge_Admin::scope_picker(); ?>
<div class="seoforge-card">
    <h2><?php esc_html_e( 'Visits — last 24 hours', 'brainwerk-seo-suite' ); ?></h2>
    <?php if ( empty( $hourly ) ) : ?>
        <p class="seoforge-empty"><?php esc_html_e( 'No visits in the last 24 hours.', 'brainwerk-seo-suite' ); ?></p>
    <?php else :
        $chart_points = array();
        foreach ( $hourly as $h ) {
            $chart_points[] = array(
                'label' => substr( $h['bucket'], -2 ) . 'h',
                'value' => (int) $h['visits'],
                /* translators: 1: hour bucket, 2: visit count */
                'tip'   => sprintf( __( '%1$s:00 · %2$s visits', 'brainwerk-seo-suite' ), $h['bucket'], number_format_i18n( (int) $h['visits'] ) ),
            );
        }
        // Inline SVG column chart. The markup is fully controlled here and every
        // dynamic value (labels, tooltips, coordinates) is esc_html'd / numeric
        // at build time, so the assembled string is output-safe. wp_kses is not
        // used because it lowercases camelCase SVG attributes (viewBox), which
        // breaks responsive scaling.
        echo '<div class="seoforge-chart-wrap">';
        echo Seoforge_Charts::bars( $chart_points ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        echo '</div>';
    endif; ?>
</div>

<div class="seoforge-card-grid">
    <div class="seoforge-card">
        <h2><?php esc_html_e( 'Browsers (7 days)', 'brainwerk-seo-suite' ); ?></h2>
        <?php $total = array_sum( array_column( $browser, 'hits' ) ) ?: 1; ?>
        <ul class="seoforge-bar-list">
            <?php foreach ( $browser as $b ) :
                $pct = round( ( (int) $b['hits'] / $total ) * 100, 1 ); ?>
                <li>
                    <span class="seoforge-bar-list-label"><?php echo esc_html( $b['label'] ?: __( 'unknown', 'brainwerk-seo-suite' ) ); ?></span>
                    <span class="seoforge-bar-list-track"><span style="width:<?php echo esc_attr( $pct ); ?>%"></span></span>
                    <span class="seoforge-bar-list-value"><?php echo esc_html( $pct ); ?>%</span>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
    <div class="seoforge-card">
        <h2><?php esc_html_e( 'Operating systems (7 days)', 'brainwerk-seo-suite' ); ?></h2>
        <?php $total = array_sum( array_column( $os, 'hits' ) ) ?: 1; ?>
        <ul class="seoforge-bar-list">
            <?php foreach ( $os as $o ) :
                $pct = round( ( (int) $o['hits'] / $total ) * 100, 1 ); ?>
                <li>
                    <span class="seoforge-bar-list-label"><?php echo esc_html( $o['label'] ?: __( 'unknown', 'brainwerk-seo-suite' ) ); ?></span>
                    <span class="seoforge-bar-list-track"><span style="width:<?php echo esc_attr( $pct ); ?>%"></span></span>
                    <span class="seoforge-bar-list-value"><?php echo esc_html( $pct ); ?>%</span>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
</div>

<div class="seoforge-card">
    <h2><?php esc_html_e( 'Visitor countries (7 days)', 'brainwerk-seo-suite' ); ?> <?php echo Seoforge_License::pro_badge(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></h2>
    <?php if ( empty( $countries ) ) : ?>
        <p class="seoforge-empty"><?php esc_html_e( 'No geolocated visits yet. Country data is filled in automatically from new visits.', 'brainwerk-seo-suite' ); ?></p>
    <?php else :
        $geo_total = array_sum( array_column( $countries, 'hits' ) ) ?: 1; ?>
        <?php if ( $geo_pro ) : ?>
            <p class="sf-geo-hint description"><?php esc_html_e( 'Click a country to see its top regions and cities.', 'brainwerk-seo-suite' ); ?></p>
        <?php else : ?>
            <?php echo Seoforge_Pro::upsell( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                __( 'Region & city analytics', 'brainwerk-seo-suite' ),
                __( 'Country-level data above stays free. Click-to-drill-down into regions and cities per country is a Pro feature.', 'brainwerk-seo-suite' )
            ); ?>
        <?php endif; ?>
        <ul class="seoforge-bar-list sf-geo-list">
            <?php foreach ( $countries as $c ) :
                $cc  = strtoupper( (string) $c['code'] );
                $pct = round( ( (int) $c['hits'] / $geo_total ) * 100, 1 ); ?>
                <li>
                    <button type="button" class="sf-geo-country" data-country="<?php echo esc_attr( $cc ); ?>" aria-expanded="false"
                        aria-label="<?php echo esc_attr( sprintf( /* translators: 1: country code, 2: visits, 3: percentage */ __( 'Show region details for %1$s — %2$s visits, %3$s%%', 'brainwerk-seo-suite' ), $cc, number_format_i18n( (int) $c['hits'] ), $pct ) ); ?>">
                        <span class="seoforge-bar-list-label">
                            <span class="sf-geo-flag"><?php echo esc_html( Seoforge_Geo::flag( $cc ) ); ?></span>
                            <span class="sf-geo-name" data-cc="<?php echo esc_attr( $cc ); ?>"><?php echo esc_html( $cc ); ?></span>
                        </span>
                        <span class="seoforge-bar-list-track"><span style="width:<?php echo esc_attr( $pct ); ?>%"></span></span>
                        <span class="seoforge-bar-list-value"><?php echo esc_html( number_format_i18n( (int) $c['hits'] ) ); ?> · <?php echo esc_html( $pct ); ?>%</span>
                    </button>
                    <div class="sf-geo-drill" hidden></div>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</div>

<div class="seoforge-card">
    <h2><?php esc_html_e( 'Recent visits', 'brainwerk-seo-suite' ); ?></h2>
    <table class="widefat striped">
        <thead>
            <tr>
                <th><?php esc_html_e( 'Time (UTC)', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Page', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Source', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Device', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Browser', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Country', 'brainwerk-seo-suite' ); ?></th>
            </tr>
        </thead>
        <tbody>
        <?php if ( empty( $recent ) ) : ?>
            <tr><td colspan="6"><?php esc_html_e( 'No visits recorded yet.', 'brainwerk-seo-suite' ); ?></td></tr>
        <?php else : foreach ( $recent as $v ) : ?>
            <tr>
                <td><?php echo esc_html( $v['created_at'] ); ?></td>
                <td><a href="<?php echo esc_url( home_url( $v['url_path'] ) ); ?>" target="_blank">
                    <?php echo esc_html( $v['title'] ?: $v['url_path'] ); ?></a></td>
                <td>
                    <span class="seoforge-pill seoforge-pill-<?php echo esc_attr( $v['source_type'] ); ?>">
                        <?php echo esc_html( $v['source_type'] ); ?>
                    </span>
                    <?php echo esc_html( $v['source_name'] ?: '' ); ?>
                </td>
                <td><?php echo esc_html( $v['device_type'] ); ?></td>
                <td><?php echo esc_html( $v['browser'] ); ?></td>
                <td>
                    <?php $vc = strtoupper( (string) ( $v['country'] ?? '' ) ); ?>
                    <?php if ( '' !== $vc ) : ?>
                        <span class="sf-geo-flag"><?php echo esc_html( Seoforge_Geo::flag( $vc ) ); ?></span>
                        <span class="sf-geo-name" data-cc="<?php echo esc_attr( $vc ); ?>"><?php echo esc_html( $vc ); ?></span>
                    <?php else : ?>
                        <span class="sf-geo-name">—</span>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; endif; ?>
        </tbody>
    </table>
</div>
