<?php
/**
 * Logs template.
 *
 * @package NaveenCodesImageOptimizer
 */

defined( 'ABSPATH' ) || exit;
(
	static function ( $banner, $logs ) {
		?>
<div class="wrap naveencodes-wrap">
	<?php include NAVEENCODES_DIR . 'templates/admin-banner.php'; ?>
	<div class="naveencodes-header">
		<div>
			<h1><?php esc_html_e( 'Logs', 'naveencodes-image-optimizer' ); ?></h1>
			<p class="naveencodes-subtitle"><?php esc_html_e( 'Review recent optimization activity and failures. Logs stay inside WordPress so admins can troubleshoot without guesswork.', 'naveencodes-image-optimizer' ); ?></p>
		</div>
		<button type="button" class="button button-secondary" data-naveencodes-clear-logs><?php esc_html_e( 'Clear Logs', 'naveencodes-image-optimizer' ); ?></button>
	</div>

	<div class="naveencodes-panel">
		<?php if ( empty( $logs ) ) : ?>
			<div class="naveencodes-empty-state">
				<p><?php esc_html_e( 'No logs recorded yet.', 'naveencodes-image-optimizer' ); ?></p>
			</div>
		<?php else : ?>
			<table class="naveencodes-logs-table">
				<thead>
					<tr>
						<th scope="col"><?php esc_html_e( 'Time', 'naveencodes-image-optimizer' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Level', 'naveencodes-image-optimizer' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Message', 'naveencodes-image-optimizer' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Context', 'naveencodes-image-optimizer' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Action', 'naveencodes-image-optimizer' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $logs as $ultra_image_optimizer_row ) : ?>
						<?php
						$context       = is_array( $ultra_image_optimizer_row['context'] ?? null ) ? $ultra_image_optimizer_row['context'] : array();
						$attachment_id = 0;
						$primary_error = '';
						$context_view  = $context;

						if ( ! empty( $context['attachment_id'] ) ) {
							$attachment_id = absint( $context['attachment_id'] );
						} elseif ( ! empty( $context['attachment'] ) ) {
							$attachment_id = absint( $context['attachment'] );
						}

						if ( ! empty( $context['error'] ) && is_scalar( $context['error'] ) ) {
							$primary_error = sanitize_text_field( (string) $context['error'] );
							unset( $context_view['error'] );
						}

						$edit_url       = $attachment_id ? get_edit_post_link( $attachment_id, '' ) : '';
						$attachment_url = $attachment_id ? wp_get_attachment_url( $attachment_id ) : '';
						?>
						<tr>
							<td><?php echo esc_html( $ultra_image_optimizer_row['timestamp'] ?? '' ); ?></td>
							<td><?php echo esc_html( strtoupper( $ultra_image_optimizer_row['level'] ?? 'info' ) ); ?></td>
							<td class="naveencodes-logs-message">
								<strong><?php echo esc_html( $ultra_image_optimizer_row['message'] ?? '' ); ?></strong>
								<?php if ( $primary_error ) : ?>
									<p class="naveencodes-inline-error"><?php echo esc_html( $primary_error ); ?></p>
								<?php endif; ?>
							</td>
							<td class="naveencodes-logs-context">
								<?php if ( empty( $context_view ) ) : ?>
									<span class="naveencodes-muted"><?php esc_html_e( 'No extra context', 'naveencodes-image-optimizer' ); ?></span>
								<?php else : ?>
									<div class="naveencodes-log-context-list">
										<?php foreach ( $context_view as $context_key => $context_value ) : ?>
											<div>
												<strong><?php echo esc_html( ucwords( str_replace( '_', ' ', (string) $context_key ) ) ); ?>:</strong>
												<span><?php echo esc_html( is_scalar( $context_value ) ? (string) $context_value : wp_json_encode( $context_value ) ); ?></span>
											</div>
										<?php endforeach; ?>
									</div>
								<?php endif; ?>
							</td>
							<td>
								<?php if ( $edit_url || $attachment_url ) : ?>
									<div class="naveencodes-logs-actions">
										<?php if ( $edit_url ) : ?>
											<a class="button button-secondary button-small" href="<?php echo esc_url( $edit_url ); ?>"><?php esc_html_e( 'Open in Media', 'naveencodes-image-optimizer' ); ?></a>
										<?php endif; ?>
										<?php if ( $attachment_url ) : ?>
											<a class="button button-secondary button-small" href="<?php echo esc_url( $attachment_url ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'View Image', 'naveencodes-image-optimizer' ); ?></a>
										<?php endif; ?>
									</div>
								<?php else : ?>
									<span class="naveencodes-muted"><?php esc_html_e( 'No image link', 'naveencodes-image-optimizer' ); ?></span>
								<?php endif; ?>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>
</div>
		<?php
	}
)( $banner, $logs );
