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

add_action( 'admin_menu', 'elementsell_updater_diagnostics_register_hidden_page' );
add_action( 'admin_post_elementsell_refresh_update_diagnostics', 'elementsell_updater_diagnostics_refresh_handle' );

/**
 * @return void
 */
function elementsell_updater_diagnostics_register_hidden_page() {
    add_submenu_page(
        null,
        'Diagnostika aktualizací',
        'Diagnostika aktualizací',
        'manage_options',
        'elementsell-updater-diagnostics',
        'elementsell_updater_diagnostics_render_page'
    );
}

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

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

    check_admin_referer( 'elementsell_refresh_update_diagnostics' );

    elementsell_updater_force_refresh_updates();

    wp_safe_redirect( elementsell_get_updater_diagnostics_admin_url() );
    exit;
}

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

    $diag = elementsell_get_plugin_update_diagnostics( true );
    ?>
    <div class="wrap">
        <h1>Diagnostika aktualizací ElementSell</h1>

        <p>
            <a class="button button-primary" href="<?php echo esc_url( admin_url( 'plugins.php' ) ); ?>">Pluginy</a>
            <a class="button" href="<?php echo esc_url( admin_url( 'update-core.php' ) ); ?>">Aktualizace</a>
        </p>

        <table class="widefat striped" style="max-width: 960px;">
            <tbody>
                <tr>
                    <th scope="row">Lokální verze</th>
                    <td><code><?php echo esc_html( (string) $diag['local_version'] ); ?></code> <span class="description">(z plugin.php na disku)</span></td>
                </tr>
                <tr>
                    <th scope="row">Verze v seznamu WP</th>
                    <td><code><?php echo esc_html( (string) ( $diag['listed_local_version'] ?? $diag['local_version'] ) ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">Konstanta PF_VERSION</th>
                    <td>
                        <code><?php echo esc_html( (string) ( $diag['pf_version_constant'] ?? '' ) ); ?></code>
                        <?php if ( isset( $diag['pf_version_matches'] ) && empty( $diag['pf_version_matches'] ) ) : ?>
                            <p class="description" style="color:#b32d2e;">Konstanta PF_VERSION neodpovídá hlavičce Version: v plugin.php — updater může mylně považovat instalaci za aktuální.</p>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Verze na serveru</th>
                    <td><code><?php echo esc_html( (string) $diag['remote_version'] ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">URL manifestu</th>
                    <td><code><?php echo esc_html( (string) $diag['manifest_url'] ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">HTTP status</th>
                    <td>
                        <?php
                        $http_code = isset( $diag['http_status'] ) ? (int) $diag['http_status'] : 0;
                        echo esc_html( $http_code > 0 ? (string) $http_code : '—' );
                        ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Poslední kontrola</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">Chyba posledního dotazu</th>
                    <td>
                        <?php
                        $error = isset( $diag['last_error'] ) ? (string) $diag['last_error'] : '';
                        echo $error !== '' ? esc_html( $error ) : '<span style="color:#008a20;">žádná</span>';
                        ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Plugin basename</th>
                    <td><code><?php echo esc_html( (string) $diag['plugin_basename'] ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">Složka pluginu</th>
                    <td>
                        <code><?php echo esc_html( (string) $diag['plugin_slug'] ); ?></code>
                        <?php if ( empty( $diag['folder_ok'] ) ) : ?>
                            <p class="description" style="color:#b32d2e;">ZIP očekává složku <code><?php echo esc_html( (string) ( $diag['expected_folder'] ?? 'ElementSell' ) ); ?>/</code>. Přejmenujte složku nebo přeinstalujte z oficiálního ZIP.</p>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Update URI</th>
                    <td><code><?php echo esc_html( (string) ( $diag['update_uri'] ?? '' ) ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">Update URI hostname</th>
                    <td>
                        <code><?php echo esc_html( (string) $diag['update_uri_hostname'] ); ?></code>
                        <?php if ( ! empty( $diag['update_uri_hostnames'] ) && is_array( $diag['update_uri_hostnames'] ) ) : ?>
                            <p class="description">Hooky: <?php echo esc_html( implode( ', ', array_map( static function ( $host ) {
                                return 'update_plugins_' . $host;
                            }, $diag['update_uri_hostnames'] ) ) ); ?></p>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Měla by být nabídnuta aktualizace?</th>
                    <td>
                        <?php if ( ! empty( $diag['would_offer_update'] ) ) : ?>
                            <strong style="color:#008a20;">Ano</strong>
                        <?php else : ?>
                            <strong>Ne</strong>
                            <?php if ( ! empty( $diag['remote_is_newer'] ) ) : ?>
                                <p class="description" style="color:#b32d2e;">Server má novější verzi než lokální instalace, ale updater aktualizaci nenabízí — zkontrolujte chybu HTTP, hooky nebo nesoulad verzí v plugin.php.</p>
                            <?php else : ?>
                                <span class="description">(lokální verze je stejná nebo vyšší než server, nebo manifest se nepodařilo načíst)</span>
                            <?php endif; ?>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">V transientu update_plugins</th>
                    <td><?php echo ! empty( $diag['in_update_transient'] ) ? 'ano' : 'ne'; ?></td>
                </tr>
                <tr>
                    <th scope="row">Download URL</th>
                    <td><code><?php echo esc_html( (string) $diag['download_url'] ); ?></code></td>
                </tr>
                <tr>
                    <th scope="row">Hooky updateru</th>
                    <td>
                        <ul style="margin:0;">
                            <?php foreach ( (array) $diag['hooks_registered'] as $hook => $priority ) : ?>
                                <li>
                                    <code><?php echo esc_html( (string) $hook ); ?></code>:
                                    <?php echo false === $priority ? 'ne' : 'ano (priorita ' . (int) $priority . ')'; ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </td>
                </tr>
            </tbody>
        </table>

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

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