<?php
/**
 * Sources view: traffic by type, by referrer host, and by search term.
 *
 * @package Brainwerk SEO Suite
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$range_hours = isset( $_GET['range'] ) ? max( 24, (int) $_GET['range'] ) : 24 * 30; // phpcs:ignore WordPress.Security.NonceVerification
$scope     = Seoforge_Utils::stats_scope();
$by_source = Seoforge_Stats::top_sources( $range_hours, 50, $scope );
$by_ref    = Seoforge_Stats::top_referrers( $range_hours, 50, $scope );
$by_term   = Seoforge_Stats::top_search_terms( $range_hours, 50, $scope );

$totals = array( 'direct' => 0, 'search' => 0, 'ai' => 0, 'social' => 0, 'referral' => 0, 'campaign' => 0, 'self' => 0 );
foreach ( $by_source as $s ) {
    $t = $s['source_type'];
    if ( ! isset( $totals[ $t ] ) ) { $totals[ $t ] = 0; }
    $totals[ $t ] += (int) $s['hits'];
}
$grand = array_sum( $totals ) ?: 1;
?>
<?php Seoforge_Admin::scope_picker(); ?>
<div class="seoforge-card">
    <h2><?php esc_html_e( 'Traffic by source type', 'brainwerk-seo-suite' ); ?></h2>
    <ul class="seoforge-bar-list">
        <?php foreach ( $totals as $type => $hits ) :
            $pct = round( ( $hits / $grand ) * 100, 1 );
            $sf_type_meta = Seoforge_Utils::source_type_meta( $type ); ?>
            <li>
                <span class="seoforge-bar-list-label">
                    <span class="seoforge-pill seoforge-pill-<?php echo esc_attr( $type ); ?>"><?php echo esc_html( $type ); ?></span>
                    <?php if ( $sf_type_meta['tip'] ) : ?>
                        <span class="seoforge-tip" tabindex="0" data-seoforge-tip="<?php echo esc_attr( $sf_type_meta['tip'] ); ?>">?</span>
                    <?php endif; ?>
                    <?php echo esc_html( number_format_i18n( $hits ) ); ?>
                </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>

<?php
// GEO / AEO — referrals coming from AI answer engines.
$ai_rows = array();
foreach ( $by_source as $s ) {
    if ( 'ai' === $s['source_type'] ) {
        $name = $s['source_name'] ?: __( 'AI', 'brainwerk-seo-suite' );
        $ai_rows[ $name ] = ( $ai_rows[ $name ] ?? 0 ) + (int) $s['hits'];
    }
}
arsort( $ai_rows );
$ai_total = array_sum( $ai_rows ) ?: 1;
?>
<div class="seoforge-card">
    <h2><?php esc_html_e( 'AI answer engines', 'brainwerk-seo-suite' ); ?>
        <span class="sf-geo-flag" title="<?php esc_attr_e( 'Generative Engine Optimization', 'brainwerk-seo-suite' ); ?>">🤖</span></h2>
    <p class="description">
        <?php esc_html_e( 'AI answer engines are assistants like ChatGPT, Gemini, Copilot, Perplexity, and Claude that answer questions directly and sometimes cite your pages as a source — a visit here means someone clicked through from one of those citations.', 'brainwerk-seo-suite' ); ?>
        <?php esc_html_e( 'Brainwerk SEO Suite automatically detects these referrals and classifies them as their own source, separate from ordinary referral traffic.', 'brainwerk-seo-suite' ); ?>
        <?php esc_html_e( 'This is often called AEO or GEO (Answer/Generative Engine Optimization) — a fast-growing discovery channel you can now measure alongside classic search traffic.', 'brainwerk-seo-suite' ); ?>
    </p>
    <?php if ( empty( $ai_rows ) ) : ?>
        <p class="seoforge-empty"><?php esc_html_e( 'No AI-engine referrals in this period yet.', 'brainwerk-seo-suite' ); ?></p>
    <?php else : ?>
        <ul class="seoforge-bar-list">
            <?php foreach ( $ai_rows as $name => $hits ) :
                $pct = round( ( $hits / $ai_total ) * 100, 1 ); ?>
                <li>
                    <span class="seoforge-bar-list-label"><?php echo esc_html( $name ); ?></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( $hits ) ); ?> · <?php echo esc_html( $pct ); ?>%</span>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</div>

<div class="seoforge-card-grid">
    <div class="seoforge-card">
        <h2><?php esc_html_e( 'Top referrers', 'brainwerk-seo-suite' ); ?></h2>
        <table class="widefat striped">
            <thead><tr><th><?php esc_html_e( 'Host', 'brainwerk-seo-suite' ); ?></th><th><?php esc_html_e( 'Type', 'brainwerk-seo-suite' ); ?></th><th><?php esc_html_e( 'Hits', 'brainwerk-seo-suite' ); ?></th></tr></thead>
            <tbody>
                <?php if ( empty( $by_ref ) ) : ?>
                    <tr><td colspan="3"><?php esc_html_e( 'No referrer data yet.', 'brainwerk-seo-suite' ); ?></td></tr>
                <?php else : foreach ( $by_ref as $r ) :
                    $sf_type_meta = Seoforge_Utils::source_type_meta( $r['source_type'] ); ?>
                    <tr>
                        <td><?php echo esc_html( $r['referrer_host'] ?: '—' ); ?>
                            <?php if ( ! empty( $r['source_name'] ) && $r['source_name'] !== $r['referrer_host'] ) : ?>
                                <small>(<?php echo esc_html( $r['source_name'] ); ?>)</small>
                            <?php endif; ?>
                        </td>
                        <td><span class="seoforge-pill seoforge-pill-<?php echo esc_attr( $r['source_type'] ); ?>">
                            <?php echo esc_html( $r['source_type'] ); ?></span>
                            <?php if ( $sf_type_meta['tip'] ) : ?>
                                <span class="seoforge-tip" tabindex="0" data-seoforge-tip="<?php echo esc_attr( $sf_type_meta['tip'] ); ?>">?</span>
                            <?php endif; ?>
                        </td>
                        <td><?php echo esc_html( number_format_i18n( $r['hits'] ) ); ?></td>
                    </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>

    <div class="seoforge-card">
        <h2><?php esc_html_e( 'Search terms', 'brainwerk-seo-suite' ); ?></h2>
        <p class="description">
            <?php esc_html_e( 'Search terms are only available when the search engine includes them in the referrer URL — most no longer do. Connect Search Console (Pro) for full keyword data.', 'brainwerk-seo-suite' ); ?>
        </p>
        <table class="widefat striped">
            <thead><tr><th><?php esc_html_e( 'Term', 'brainwerk-seo-suite' ); ?></th><th><?php esc_html_e( 'Engine', 'brainwerk-seo-suite' ); ?></th><th><?php esc_html_e( 'Hits', 'brainwerk-seo-suite' ); ?></th></tr></thead>
            <tbody>
                <?php if ( empty( $by_term ) ) : ?>
                    <tr><td colspan="3"><?php esc_html_e( 'No search-term data yet.', 'brainwerk-seo-suite' ); ?></td></tr>
                <?php else : foreach ( $by_term as $t ) : ?>
                    <tr>
                        <td><?php echo esc_html( $t['search_term'] ); ?></td>
                        <td><?php echo esc_html( $t['source_name'] ); ?></td>
                        <td><?php echo esc_html( number_format_i18n( $t['hits'] ) ); ?></td>
                    </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
</div>
