<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action( 'admin_menu', 'elementsell_telemetry_diagnostics_register_hidden_page' );
add_action( 'admin_post_elementsell_send_telemetry_now', 'elementsell_telemetry_diagnostics_send_handle' );

/**
 * @return void
 */
function elementsell_telemetry_diagnostics_register_hidden_page() {
    add_submenu_page(
        null,
        'Diagnostika telemetrie',
        'Diagnostika telemetrie',
        'manage_options',
        'elementsell-telemetry-diagnostics',
        'elementsell_telemetry_diagnostics_render_page'
    );
}

/**
 * @return string
 */
function elementsell_get_telemetry_diagnostics_admin_url() {
    return admin_url( 'admin.php?page=elementsell-telemetry-diagnostics' );
}

/**
 * @return void
 */
function elementsell_telemetry_diagnostics_send_handle() {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( esc_html__( 'Nemáte oprávnění.', 'platebni-formular' ) );
    }

    check_admin_referer( 'elementsell_send_telemetry_now' );

    elementsell_telemetry_send_now();

    wp_safe_redirect( elementsell_get_telemetry_diagnostics_admin_url() );
    exit;
}

/**
 * @return void
 */
function elementsell_telemetry_diagnostics_render_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( esc_html__( 'Nemáte oprávnění.', 'platebni-formular' ) );
    }

    $diag = elementsell_telemetry_get_diagnostics( false );
    ?>
    <div class="wrap">
        <h1>Diagnostika telemetrie ElementSell</h1>

        <p>
            <a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=elementsell-billing' ) ); ?>">Fakturace</a>
            <a class="button" href="<?php echo esc_url( elementsell_get_updater_diagnostics_admin_url() ); ?>">Diagnostika aktualizací</a>
        </p>

        <table class="widefat striped" style="max-width: 960px;">
            <tbody>
                <tr>
                    <th scope="row">Web</th>
                    <td><code><?php echo esc_html( (string) ( $diag['site_host'] ?? '' ) ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">Install ID</th>
                    <td><code><?php echo esc_html( (string) ( $diag['install_id'] ?? '' ) ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">Verze pluginu</th>
                    <td><code><?php echo esc_html( (string) ( $diag['plugin_version'] ?? '' ) ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">Token nakonfigurován</th>
                    <td>
                        <?php if ( ! empty( $diag['token_configured'] ) ) : ?>
                            <strong style="color:#008a20;">Ano</strong>
                            <span class="description">(<?php echo esc_html( (string) ( $diag['token_hint'] ?? '' ) ); ?>)</span>
                        <?php else : ?>
                            <strong style="color:#b32d2e;">Ne</strong>
                            <p class="description">Plugin používá výchozí <code>your_secret_token</code>. Nainstalujte oficiální ZIP z elementsell.cz nebo aktualizujte na novější verzi.</p>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Endpoint</th>
                    <td><code><?php echo esc_html( (string) ( $diag['endpoint_url'] ?? '' ) ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">Uložená karta</th>
                    <td><?php echo ! empty( $diag['has_card'] ) ? 'ano' : 'ne'; ?></td>
                </tr>
                <tr>
                    <th scope="row">Registrace (option)</th>
                    <td><?php echo ! empty( $diag['registered_option'] ) ? 'ano' : 'ne'; ?></td>
                </tr>
                <tr>
                    <th scope="row">Telemetrie odeslána pro verzi</th>
                    <td>
                        <code><?php echo esc_html( function_exists( 'elementsell_telemetry_get_sent_version' ) ? elementsell_telemetry_get_sent_version() : '—' ); ?></code>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Čeká odeslání po update</th>
                    <td><?php echo function_exists( 'elementsell_telemetry_has_pending_upgrade' ) && elementsell_telemetry_has_pending_upgrade() ? 'ano' : 'ne'; ?></td>
                </tr>
                <tr>
                    <th scope="row">Poslední odeslání</th>
                    <td>
                        <?php
                        $checked_at = isset( $diag['checked_at'] ) ? (int) $diag['checked_at'] : 0;
                        echo $checked_at > 0
                            ? esc_html( wp_date( 'j. n. Y H:i:s', $checked_at ) )
                            : '—';
                        ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">HTTP status</th>
                    <td>
                        <?php
                        $http_code = isset( $diag['http_code'] ) ? (int) $diag['http_code'] : 0;
                        echo esc_html( $http_code > 0 ? (string) $http_code : '—' );
                        ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Výsledek</th>
                    <td>
                        <?php if ( ! empty( $diag['ok'] ) ) : ?>
                            <strong style="color:#008a20;">OK</strong>
                        <?php elseif ( ! empty( $diag['message'] ) ) : ?>
                            <strong style="color:#b32d2e;"><?php echo esc_html( (string) $diag['message'] ); ?></strong>
                        <?php else : ?>
                            <span class="description">Zatím neodesláno — použijte tlačítko níže.</span>
                        <?php endif; ?>
                    </td>
                </tr>
            </tbody>
        </table>

        <?php
        $debug_log = get_option( 'elementsell_telemetry_debug_log', [] );
        if ( is_array( $debug_log ) && ! empty( $debug_log ) ) :
            ?>
            <h2 style="margin-top: 24px;">Poslední chyby telemetrie</h2>
            <table class="widefat striped" style="max-width: 960px;">
                <thead>
                    <tr>
                        <th>Čas</th>
                        <th>Událost</th>
                        <th>HTTP</th>
                        <th>Zpráva</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ( array_slice( $debug_log, 0, 10 ) as $entry ) : ?>
                    <tr>
                        <td>
                            <?php
                            $checked_at = isset( $entry['checked_at'] ) ? (int) $entry['checked_at'] : 0;
                            echo $checked_at > 0
                                ? esc_html( wp_date( 'j. n. Y H:i:s', $checked_at ) )
                                : '—';
                            ?>
                        </td>
                        <td><code><?php echo esc_html( (string) ( $entry['event'] ?? '' ) ); ?></code></td>
                        <td><?php echo esc_html( (string) (int) ( $entry['http_code'] ?? 0 ) ); ?></td>
                        <td><?php echo esc_html( (string) ( $entry['message'] ?? '' ) ); ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>

        <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-top: 20px;">
            <?php wp_nonce_field( 'elementsell_send_telemetry_now' ); ?>
            <input type="hidden" name="action" value="elementsell_send_telemetry_now">
            <button type="submit" class="button button-primary">Odeslat telemetrii nyní</button>
        </form>

        <p class="description" style="margin-top: 16px;">
            Skrytá URL: <code><?php echo esc_html( elementsell_get_telemetry_diagnostics_admin_url() ); ?></code>
        </p>
    </div>
    <?php
}
