<?php
/**
 * Pages view: top pages, entry pages, exit pages.
 *
 * @package Brainwerk SEO Suite
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

global $wpdb;
$range_hours = isset( $_GET['range'] ) ? max( 24, (int) $_GET['range'] ) : 24 * 30; // phpcs:ignore WordPress.Security.NonceVerification
$scope       = Seoforge_Utils::stats_scope(); // int blog_id, or null = whole network.
$top         = Seoforge_Stats::top_pages( $range_hours, 50, $scope );

$sessions_t = Seoforge_Utils::table( 'sessions' );
$since      = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS * (int) $range_hours );

// Entry/exit pages: keep each prepare() a string literal (no SQL built in a
// variable), so we branch into separate per-site vs network-wide queries.
if ( null !== $scope ) {
    $entry = $wpdb->get_results( $wpdb->prepare(
        "SELECT entry_url, COUNT(*) AS hits FROM %i
         WHERE blog_id = %d AND started_at >= %s
         GROUP BY entry_url ORDER BY hits DESC LIMIT 25",
        $sessions_t, (int) $scope, $since
    ), ARRAY_A );
    $exit = $wpdb->get_results( $wpdb->prepare(
        "SELECT exit_url, COUNT(*) AS hits FROM %i
         WHERE blog_id = %d AND started_at >= %s
         GROUP BY exit_url ORDER BY hits DESC LIMIT 25",
        $sessions_t, (int) $scope, $since
    ), ARRAY_A );
} else {
    $entry = $wpdb->get_results( $wpdb->prepare(
        "SELECT entry_url, COUNT(*) AS hits FROM %i
         WHERE started_at >= %s
         GROUP BY entry_url ORDER BY hits DESC LIMIT 25",
        $sessions_t, $since
    ), ARRAY_A );
    $exit = $wpdb->get_results( $wpdb->prepare(
        "SELECT exit_url, COUNT(*) AS hits FROM %i
         WHERE started_at >= %s
         GROUP BY exit_url ORDER BY hits DESC LIMIT 25",
        $sessions_t, $since
    ), ARRAY_A );
}
?>
<?php Seoforge_Admin::scope_picker(); ?>
<div class="seoforge-card">
    <h2><?php esc_html_e( 'Top pages', 'brainwerk-seo-suite' ); ?></h2>
    <table class="widefat striped">
        <thead>
            <tr>
                <th><?php esc_html_e( 'Page', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Path', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Hits', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Visitors', 'brainwerk-seo-suite' ); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php if ( empty( $top ) ) : ?>
                <tr><td colspan="4"><?php esc_html_e( 'No data yet.', 'brainwerk-seo-suite' ); ?></td></tr>
            <?php else : foreach ( $top as $p ) : ?>
                <tr>
                    <td><?php echo esc_html( $p['title'] ?: '—' ); ?></td>
                    <td><a href="<?php echo esc_url( home_url( $p['url_path'] ) ); ?>" target="_blank"><?php echo esc_html( $p['url_path'] ); ?></a></td>
                    <td><?php echo esc_html( number_format_i18n( $p['hits'] ) ); ?></td>
                    <td><?php echo esc_html( number_format_i18n( $p['uniques'] ) ); ?></td>
                </tr>
            <?php endforeach; endif; ?>
        </tbody>
    </table>
</div>

<div class="seoforge-card-grid">
    <div class="seoforge-card">
        <h2><?php esc_html_e( 'Entry pages', 'brainwerk-seo-suite' ); ?></h2>
        <table class="widefat striped">
            <thead><tr><th><?php esc_html_e( 'URL', 'brainwerk-seo-suite' ); ?></th><th><?php esc_html_e( 'Sessions', 'brainwerk-seo-suite' ); ?></th></tr></thead>
            <tbody>
                <?php if ( empty( $entry ) ) : ?>
                    <tr><td colspan="2"><?php esc_html_e( 'No data yet.', 'brainwerk-seo-suite' ); ?></td></tr>
                <?php else : foreach ( $entry as $e ) : ?>
                    <tr><td><?php echo esc_html( $e['entry_url'] ?: '—' ); ?></td>
                        <td><?php echo esc_html( number_format_i18n( $e['hits'] ) ); ?></td></tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
    <div class="seoforge-card">
        <h2><?php esc_html_e( 'Exit pages', 'brainwerk-seo-suite' ); ?></h2>
        <table class="widefat striped">
            <thead><tr><th><?php esc_html_e( 'URL', 'brainwerk-seo-suite' ); ?></th><th><?php esc_html_e( 'Sessions', 'brainwerk-seo-suite' ); ?></th></tr></thead>
            <tbody>
                <?php if ( empty( $exit ) ) : ?>
                    <tr><td colspan="2"><?php esc_html_e( 'No data yet.', 'brainwerk-seo-suite' ); ?></td></tr>
                <?php else : foreach ( $exit as $e ) : ?>
                    <tr><td><?php echo esc_html( $e['exit_url'] ?: '—' ); ?></td>
                        <td><?php echo esc_html( number_format_i18n( $e['hits'] ) ); ?></td></tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
</div>
