<?php
/**
 * Global Pulse section for Noesia Smart Waiter.
 *
 * @package Noesia_Smart_Waiter
 */

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

$noesia_swa_global_pulse_service = null;
if ( function_exists( 'noesia_swa' ) ) {
    $noesia_swa_global_pulse_service = noesia_swa()->global_pulse_data;
}
if ( ! ( $noesia_swa_global_pulse_service instanceof Noesia_SWA_Global_Pulse_Data ) ) {
    $noesia_swa_global_pulse_service = new Noesia_SWA_Global_Pulse_Data();
}

$noesia_swa_global_pulse_context = $noesia_swa_global_pulse_service->get_context();
if ( is_array( $noesia_swa_global_pulse_context ) ) {
    extract( $noesia_swa_global_pulse_context, EXTR_SKIP );
}
?>

<div class="noesia-swa-section-grid noesia-swa-global-pulse-grid">
    <div class="noesia-swa-card noesia-swa-card--span2 noesia-swa-global-pulse-shell">
        <div class="noesia-swa-hero-card noesia-swa-global-pulse-hero <?php echo esc_attr( $all_clear ? 'is-clear' : 'has-alert' ); ?>">
            <div class="noesia-swa-hero-icon noesia-swa-global-pulse-hero-icon" aria-hidden="true">
                <span class="dashicons dashicons-heart"></span>
            </div>

            <div>
                <div class="noesia-swa-kicker"><?php esc_html_e( 'GLOBAL PULSE', 'noesia-smart-waiter' ); ?></div>
                <h3><?php esc_html_e( 'Global Pulse', 'noesia-smart-waiter' ); ?></h3>
                <p class="noesia-swa-muted"><?php esc_html_e( 'Planet observances, WordPress update awareness and detailed server diagnostics in one live operational view.', 'noesia-smart-waiter' ); ?></p>
            </div>

            <div class="noesia-swa-mini-stats noesia-swa-global-pulse-summary">
                <div>
                    <strong><?php echo esc_html( $pending_total ); ?></strong>
                    <span><?php esc_html_e( 'Pending updates', 'noesia-smart-waiter' ); ?></span>
                </div>

                <div>
                    <strong><?php echo esc_html( get_bloginfo( 'version' ) ); ?></strong>
                    <span><?php esc_html_e( 'WP core', 'noesia-smart-waiter' ); ?></span>
                </div>

                <div>
                    <strong><?php echo esc_html( PHP_VERSION ); ?></strong>
                    <span><?php esc_html_e( 'PHP', 'noesia-smart-waiter' ); ?></span>
                </div>

                <div>
                    <strong><?php echo esc_html( noesia_swa_global_pulse_time_ago_label( isset( $update_snapshot['latest_check'] ) ? absint( $update_snapshot['latest_check'] ) : 0 ) ); ?></strong>
                    <span><?php esc_html_e( 'Last scan', 'noesia-smart-waiter' ); ?></span>
                </div>
            </div>
        </div>
    </div>

    <div class="noesia-swa-card noesia-swa-global-pulse-calendar-card">
        <div class="noesia-swa-card-header">
            <div>
                <div class="noesia-swa-kicker"><?php esc_html_e( 'PLANET CALENDAR', 'noesia-smart-waiter' ); ?></div>
                <h3><?php esc_html_e( 'Global Days', 'noesia-smart-waiter' ); ?></h3>
                <p class="noesia-swa-muted"><?php esc_html_e( 'A curated set of widely recognized international observances relevant to hospitality, culture and daily engagement.', 'noesia-smart-waiter' ); ?></p>
            </div>
        </div>

        <?php if ( is_array( $next_planet_day ) ) : ?>
            <div class="noesia-swa-note-box noesia-swa-global-pulse-next-day">
                <strong><?php esc_html_e( 'Next notable date', 'noesia-smart-waiter' ); ?></strong>
                <span><?php echo esc_html( $next_planet_day['date_label'] ); ?></span>
                <p class="noesia-swa-muted"><?php echo esc_html( $next_planet_day['title'] ); ?></p>
            </div>
        <?php endif; ?>

        <div class="noesia-swa-code-list noesia-swa-global-pulse-calendar-list">
            <?php foreach ( $planet_days as $noesia_swa_planet_day ) : ?>
                <div class="noesia-swa-code-item">
                    <strong><?php echo esc_html( $noesia_swa_planet_day['date_label'] ); ?></strong>
                    <p class="noesia-swa-muted"><?php echo esc_html( $noesia_swa_planet_day['title'] ); ?></p>
                    <p class="noesia-swa-muted"><?php echo esc_html( $noesia_swa_planet_day['note'] ); ?></p>
                </div>
            <?php endforeach; ?>
        </div>
    </div>

    <div class="noesia-swa-card noesia-swa-global-pulse-updates-card">
        <div class="noesia-swa-card-header">
            <div>
                <div class="noesia-swa-kicker"><?php esc_html_e( 'WORDPRESS PULSE', 'noesia-smart-waiter' ); ?></div>
                <h3><?php esc_html_e( 'Update Snapshot', 'noesia-smart-waiter' ); ?></h3>
                <p class="noesia-swa-muted"><?php esc_html_e( 'Reads the existing WordPress update transients and shows what is waiting across core, plugins, themes and language packs.', 'noesia-smart-waiter' ); ?></p>
            </div>
        </div>

        <div class="noesia-swa-note-box noesia-swa-global-pulse-check-note">
            <strong><?php esc_html_e( 'Latest recorded check', 'noesia-smart-waiter' ); ?></strong>
            <span><?php echo esc_html( noesia_swa_global_pulse_time_ago_label( isset( $update_snapshot['latest_check'] ) ? absint( $update_snapshot['latest_check'] ) : 0 ) ); ?></span>
            <p class="noesia-swa-muted"><?php esc_html_e( 'This panel does not force a remote check. It reports the latest data already stored by WordPress.', 'noesia-smart-waiter' ); ?></p>
        </div>

        <div class="noesia-swa-section-grid noesia-swa-global-pulse-update-grid">
            <?php foreach ( $update_snapshot['groups'] as $noesia_swa_group ) : ?>
                <?php
                $noesia_swa_group_count   = isset( $noesia_swa_group['count'] ) ? absint( $noesia_swa_group['count'] ) : 0;
                $noesia_swa_status_text   = ( 0 === $noesia_swa_group_count )
                    ? __( 'Up to date', 'noesia-smart-waiter' )
                    : sprintf(
                        /* translators: %d: pending update count. */
                        _n( '%d update pending', '%d updates pending', $noesia_swa_group_count, 'noesia-smart-waiter' ),
                        $noesia_swa_group_count
                    );
                $noesia_swa_detail_text   = ( 0 === $noesia_swa_group_count )
                    ? ( isset( $noesia_swa_group['empty_detail'] ) ? (string) $noesia_swa_group['empty_detail'] : '' )
                    : ( isset( $noesia_swa_group['detail'] ) ? (string) $noesia_swa_group['detail'] : '' );
                $noesia_swa_checked_label = noesia_swa_global_pulse_time_ago_label( isset( $noesia_swa_group['checked'] ) ? absint( $noesia_swa_group['checked'] ) : 0 );
                ?>
                <div class="noesia-swa-asset-box noesia-swa-global-pulse-update-box <?php echo esc_attr( 0 === $noesia_swa_group_count ? 'is-clear' : 'has-alert' ); ?>">
                    <strong><?php echo esc_html( $noesia_swa_group['title'] ); ?></strong>
                    <span><?php echo esc_html( $noesia_swa_status_text ); ?></span>
                    <p class="noesia-swa-muted"><?php echo esc_html( $noesia_swa_detail_text ); ?></p>
                    <p class="noesia-swa-muted"><?php echo esc_html( $noesia_swa_checked_label ); ?></p>
                </div>
            <?php endforeach; ?>
        </div>
    </div>

    <div class="noesia-swa-card noesia-swa-card--span2 noesia-swa-global-pulse-server-card">
        <div class="noesia-swa-card-header">
            <div>
                <div class="noesia-swa-kicker"><?php esc_html_e( 'SERVER PROFILE', 'noesia-smart-waiter' ); ?></div>
                <h3><?php esc_html_e( 'System Environment', 'noesia-smart-waiter' ); ?></h3>
                <p class="noesia-swa-muted"><?php esc_html_e( 'Operational diagnostics for the current WordPress installation, PHP runtime and site environment.', 'noesia-smart-waiter' ); ?></p>
            </div>
        </div>

        <div class="noesia-swa-mini-stats noesia-swa-global-pulse-server-grid">
            <?php foreach ( $server_rows as $noesia_swa_row ) : ?>
                <div class="noesia-swa-global-pulse-server-item">
                    <strong><?php echo esc_html( $noesia_swa_row['label'] ); ?></strong>
                    <span><?php echo esc_html( (string) $noesia_swa_row['value'] ); ?></span>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
