<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.WP.GlobalVariablesOverride.Prohibited -- False positives: locally-scoped variables (template view-vars via TemplateLoader::render() extract+include within a method, or closure locals), not plugin globals.
defined( 'ABSPATH' ) || exit;

$entries = is_array( $entries ?? null ) ? $entries : [];
$per_page = isset( $per_page ) ? (int) $per_page : ( isset( $lines ) ? (int) $lines : 400 );
$current_page = isset( $current_page ) ? (int) $current_page : 1;
$total_pages = isset( $total_pages ) ? (int) $total_pages : 1;
$total_lines = isset( $total_lines ) ? (int) $total_lines : count( $entries );
$enabled = ! empty( $enabled );
$file_exists = ! empty( $file_exists );
$readable = ! empty( $readable );
$file_size = isset( $file_size ) ? (int) $file_size : 0;
$file_mtime = isset( $file_mtime ) ? (int) $file_mtime : 0;
$path = (string) ( $path ?? '' );
$settings_url = (string) ( $settings_url ?? '' );
$notice = (string) ( $notice ?? '' );
$notice_type = (string) ( $notice_type ?? 'success' );

$per_page_options = [ 100, 200, 400, 800, 1200, 2000 ];
if ( ! in_array( $per_page, $per_page_options, true ) ) {
    $per_page_options[] = $per_page;
    sort( $per_page_options );
}

$current_page = max( 1, $current_page );
$total_pages = max( 1, $total_pages );
if ( $current_page > $total_pages ) {
    $current_page = $total_pages;
}
?>

<div class="wrap bookaro-admin bookaro-admin-logs">
    <div class="bookaro-card bookaro-hero bookaro-system-hero">
        <div class="bookaro-hero__content">
            <div class="bookaro-hero__brand">
                <img src="<?php echo esc_url( BOOKARO_PLUGIN_URL . 'assets/img/mb-logo.svg' ); ?>" alt="<?php esc_attr_e( 'MyBookaro', 'mybookaro' ); ?>" width="120" height="36" />
            </div>
            <h1><?php esc_html_e( 'Plugin Logs', 'mybookaro' ); ?></h1>
            <p class="description"><?php esc_html_e( 'Inspect recent MyBookaro log entries, download diagnostics, or clear logs after resolving issues.', 'mybookaro' ); ?></p>
        </div>
        <div class="bookaro-hero__actions">
            <?php if ( $settings_url ) : ?>
                <a class="button button-secondary" href="<?php echo esc_url( $settings_url ); ?>">
                    <?php esc_html_e( 'Open Notifications Settings', 'mybookaro' ); ?>
                </a>
            <?php endif; ?>
        </div>
    </div>

    <?php if ( $notice !== '' ) : ?>
        <div class="notice <?php echo esc_attr( $notice_type === 'error' ? 'notice-error' : 'notice-success' ); ?>">
            <p><?php echo esc_html( $notice ); ?></p>
        </div>
    <?php endif; ?>

    <div class="bookaro-card bookaro-system-card">
        <div class="bookaro-card__header">
            <h2><?php esc_html_e( 'Log Status', 'mybookaro' ); ?></h2>
        </div>
        <div class="bookaro-health__list">
            <div class="bookaro-health__item">
                <div class="bookaro-health__label"><?php esc_html_e( 'File logging', 'mybookaro' ); ?></div>
                <span class="bookaro-status-pill <?php echo esc_attr( $enabled ? 'bookaro-status-pill--ok' : 'bookaro-status-pill--warn' ); ?>">
                    <?php echo esc_html( $enabled ? __( 'Enabled', 'mybookaro' ) : __( 'Disabled', 'mybookaro' ) ); ?>
                </span>
                <p class="bookaro-health__message">
                    <?php
                    if ( $enabled ) {
                        esc_html_e( 'MyBookaro is writing errors to a dedicated log file.', 'mybookaro' );
                    } else {
                        esc_html_e( 'Enable file logging to see entries here.', 'mybookaro' );
                    }
                    ?>
                </p>
            </div>
            <div class="bookaro-health__item">
                <div class="bookaro-health__label"><?php esc_html_e( 'Log file', 'mybookaro' ); ?></div>
                <span class="bookaro-status-pill <?php echo esc_attr( $file_exists ? 'bookaro-status-pill--ok' : 'bookaro-status-pill--warn' ); ?>">
                    <?php echo esc_html( $file_exists ? __( 'Available', 'mybookaro' ) : __( 'Not found', 'mybookaro' ) ); ?>
                </span>
                <div class="bookaro-health__message">
                    <?php if ( $path !== '' ) : ?>
                        <div class="bookaro-log-meta">
                            <div><strong><?php esc_html_e( 'Path:', 'mybookaro' ); ?></strong> <code><?php echo esc_html( $path ); ?></code></div>
                            <div><strong><?php esc_html_e( 'Size:', 'mybookaro' ); ?></strong> <?php echo esc_html( size_format( $file_size ) ); ?></div>
                            <div><strong><?php esc_html_e( 'Updated:', 'mybookaro' ); ?></strong>
                                <?php echo esc_html( $file_mtime ? date_i18n( 'Y-m-d H:i:s', $file_mtime ) : __( 'N/A', 'mybookaro' ) ); ?>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <div class="bookaro-actions bookaro-log-actions">
            <a class="button button-secondary" href="<?php echo esc_url( admin_url( 'admin.php?page=bookaro-logs' ) ); ?>">
                <?php esc_html_e( 'Refresh', 'mybookaro' ); ?>
            </a>
            <a class="button button-secondary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=bookaro-logs&action=download' ), 'bookaro_download_logs' ) ); ?>">
                <?php esc_html_e( 'Download Log', 'mybookaro' ); ?>
            </a>
            <a class="button button-secondary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=bookaro-logs&action=clear' ), 'bookaro_clear_logs' ) ); ?>" onclick="return confirm('<?php echo esc_js( __( 'Clear all log entries?', 'mybookaro' ) ); ?>');">
                <?php esc_html_e( 'Clear Log', 'mybookaro' ); ?>
            </a>
        </div>
    </div>

    <div class="bookaro-card bookaro-system-card">
        <div class="bookaro-card__header">
            <h2><?php esc_html_e( 'Recent Entries', 'mybookaro' ); ?></h2>
        </div>
        <form method="get" class="bookaro-log-controls">
            <input type="hidden" name="page" value="bookaro-logs">
            <div class="tablenav top">
                <div class="alignleft actions bookaro-per-page">
                    <label class="screen-reader-text" for="bookaro-log-per-page-top"><?php esc_html_e( 'Lines per page', 'mybookaro' ); ?></label>
                    <select name="per_page" id="bookaro-log-per-page-top" onchange="this.form.querySelectorAll('input[name=&quot;paged&quot;]').forEach(function(el){el.value='1';});this.form.submit();">
                        <?php foreach ( $per_page_options as $opt ) : ?>
                            <option value="<?php echo esc_attr( $opt ); ?>" <?php selected( $per_page, $opt ); ?>>
                                <?php echo esc_html( $opt ); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <span class="description"><?php esc_html_e( 'per page', 'mybookaro' ); ?></span>
                </div>
                <div class="tablenav-pages<?php echo esc_attr( $total_pages <= 1 ? ' one-page' : '' ); ?>">
                    <span class="displaying-num">
                        <?php
                        printf(
                            /* translators: %s: number of log lines. */
                            esc_html( _n( '%s line', '%s lines', $total_lines, 'mybookaro' ) ),
                            esc_html( number_format_i18n( $total_lines ) )
                        );
                        ?>
                    </span>
                    <span class="pagination-links">
                        <?php
                        $base_url = admin_url( 'admin.php?page=bookaro-logs' );
                        $base_url = add_query_arg( [ 'per_page' => $per_page ], $base_url );
                        $prev_page = max( 1, $current_page - 1 );
                        $next_page = min( $total_pages, $current_page + 1 );
                        $disable_first = $current_page <= 1;
                        $disable_last = $current_page >= $total_pages;

                        if ( $disable_first ) {
                            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&laquo;</span>';
                            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&lsaquo;</span>';
                        } else {
                            printf(
                                '<a class="first-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&laquo;</span></a>',
                                esc_url( add_query_arg( 'paged', 1, $base_url ) ),
                                esc_html__( 'First page', 'mybookaro' )
                            );
                            printf(
                                '<a class="prev-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&lsaquo;</span></a>',
                                esc_url( add_query_arg( 'paged', $prev_page, $base_url ) ),
                                esc_html__( 'Previous page', 'mybookaro' )
                            );
                        }
                        ?>
                        <span class="paging-input">
                            <label for="bookaro-log-page" class="screen-reader-text"><?php esc_html_e( 'Current Page', 'mybookaro' ); ?></label>
                            <input class="current-page" id="bookaro-log-page" type="text" name="paged" value="<?php echo esc_attr( (string) $current_page ); ?>" size="1" aria-describedby="table-paging" />
                            <span class="tablenav-paging-text">
                                <?php
                                printf(
                                    /* translators: %s: total pages. */
                                    esc_html__( 'of %s', 'mybookaro' ),
                                    '<span class="total-pages">' . esc_html( number_format_i18n( $total_pages ) ) . '</span>'
                                );
                                ?>
                            </span>
                        </span>
                        <?php
                        if ( $disable_last ) {
                            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&rsaquo;</span>';
                            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&raquo;</span>';
                        } else {
                            printf(
                                '<a class="next-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&rsaquo;</span></a>',
                                esc_url( add_query_arg( 'paged', $next_page, $base_url ) ),
                                esc_html__( 'Next page', 'mybookaro' )
                            );
                            printf(
                                '<a class="last-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&raquo;</span></a>',
                                esc_url( add_query_arg( 'paged', $total_pages, $base_url ) ),
                                esc_html__( 'Last page', 'mybookaro' )
                            );
                        }
                        ?>
                    </span>
                </div>
            </div>
        <?php if ( ! $enabled ) : ?>
            <p class="description">
                <?php esc_html_e( 'File logging is currently disabled. Enable it in Notifications settings to capture entries here.', 'mybookaro' ); ?>
            </p>
        <?php elseif ( ! $file_exists ) : ?>
            <p class="description">
                <?php esc_html_e( 'No log file found yet. Trigger an error or save a booking to generate logs.', 'mybookaro' ); ?>
            </p>
        <?php elseif ( ! $readable ) : ?>
            <p class="description">
                <?php esc_html_e( 'The log file exists but cannot be read. Check file permissions.', 'mybookaro' ); ?>
            </p>
        <?php elseif ( empty( $entries ) ) : ?>
            <p class="description">
                <?php esc_html_e( 'No log entries recorded yet.', 'mybookaro' ); ?>
            </p>
        <?php else : ?>
            <div class="bookaro-log-viewer" role="region" aria-label="<?php esc_attr_e( 'MyBookaro log entries', 'mybookaro' ); ?>">
                <pre><?php echo esc_html( implode( "\n", $entries ) ); ?></pre>
            </div>
        <?php endif; ?>
            <div class="tablenav bottom">
                <div class="alignleft actions bookaro-per-page">
                    <label class="screen-reader-text" for="bookaro-log-per-page-bottom"><?php esc_html_e( 'Lines per page', 'mybookaro' ); ?></label>
                    <select name="per_page" id="bookaro-log-per-page-bottom" onchange="this.form.querySelectorAll('input[name=&quot;paged&quot;]').forEach(function(el){el.value='1';});this.form.submit();">
                        <?php foreach ( $per_page_options as $opt ) : ?>
                            <option value="<?php echo esc_attr( $opt ); ?>" <?php selected( $per_page, $opt ); ?>>
                                <?php echo esc_html( $opt ); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <span class="description"><?php esc_html_e( 'per page', 'mybookaro' ); ?></span>
                </div>
                <div class="tablenav-pages<?php echo esc_attr( $total_pages <= 1 ? ' one-page' : '' ); ?>">
                    <span class="displaying-num">
                        <?php
                        printf(
                            /* translators: %s: number of log lines. */
                            esc_html( _n( '%s line', '%s lines', $total_lines, 'mybookaro' ) ),
                            esc_html( number_format_i18n( $total_lines ) )
                        );
                        ?>
                    </span>
                    <span class="pagination-links">
                        <?php
                        if ( $disable_first ) {
                            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&laquo;</span>';
                            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&lsaquo;</span>';
                        } else {
                            printf(
                                '<a class="first-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&laquo;</span></a>',
                                esc_url( add_query_arg( 'paged', 1, $base_url ) ),
                                esc_html__( 'First page', 'mybookaro' )
                            );
                            printf(
                                '<a class="prev-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&lsaquo;</span></a>',
                                esc_url( add_query_arg( 'paged', $prev_page, $base_url ) ),
                                esc_html__( 'Previous page', 'mybookaro' )
                            );
                        }
                        ?>
                        <span class="paging-input">
                            <label for="bookaro-log-page-bottom" class="screen-reader-text"><?php esc_html_e( 'Current Page', 'mybookaro' ); ?></label>
                            <input class="current-page" id="bookaro-log-page-bottom" type="text" name="paged" value="<?php echo esc_attr( (string) $current_page ); ?>" size="1" aria-describedby="table-paging" />
                            <span class="tablenav-paging-text">
                                <?php
                                printf(
                                    /* translators: %s: total pages. */
                                    esc_html__( 'of %s', 'mybookaro' ),
                                    '<span class="total-pages">' . esc_html( number_format_i18n( $total_pages ) ) . '</span>'
                                );
                                ?>
                            </span>
                        </span>
                        <?php
                        if ( $disable_last ) {
                            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&rsaquo;</span>';
                            echo '<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&raquo;</span>';
                        } else {
                            printf(
                                '<a class="next-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&rsaquo;</span></a>',
                                esc_url( add_query_arg( 'paged', $next_page, $base_url ) ),
                                esc_html__( 'Next page', 'mybookaro' )
                            );
                            printf(
                                '<a class="last-page button" href="%s"><span class="screen-reader-text">%s</span><span aria-hidden="true">&raquo;</span></a>',
                                esc_url( add_query_arg( 'paged', $total_pages, $base_url ) ),
                                esc_html__( 'Last page', 'mybookaro' )
                            );
                        }
                        ?>
                    </span>
                </div>
            </div>
        </form>
    </div>
</div>
