<?php
/**
 * Admin view: activity log.
 *
 * @package BitForgeNet_Guest_Tickets
 */

defined( 'ABSPATH' ) || exit;

// This file is a template: it is included from a method -- never at file
// scope -- so everything declared here belongs to that method's variable
// scope and not to the global one. PHPCS reads each file on its own and has
// no way to see where the include happens, so it reads these as globals.
// Stated once, for the whole file, per this plugin's suppression rule.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$level = isset( $_GET['level'] ) ? sanitize_key( wp_unslash( $_GET['level'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filters.
$paged = isset( $_GET['paged'] ) ? max( 1, (int) $_GET['paged'] ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only filters.

$per_page = 50;
$rows     = BFNET_Log::recent(
	array(
		'level'  => $level,
		'limit'  => $per_page,
		'offset' => ( $paged - 1 ) * $per_page,
	)
);

$colors = array(
	'info'     => '#5f6368',
	'warning'  => '#b26b00',
	'error'    => '#c5221f',
	'security' => '#8430ce',
);
?>
<div class="wrap bfnet-admin">

	<?php
	$bfnet_title    = __( 'Activity log', 'bitforgenet-guest-tickets' );
	$bfnet_subtitle = __( 'Who did what, and when. Payments validated, tickets voided, security events — all of it is recorded.', 'bitforgenet-guest-tickets' );
	include BFNET_PATH . 'admin/views/partial-masthead.php';
	?>

	<div class="bfnet-panel">
		<div class="bfnet-panel__head">
			<form method="get" class="bfnet-filters">
				<input type="hidden" name="page" value="bitforgenet-guest-tickets-logs">
				<div class="bfnet-field">
					<label class="bfnet-label" for="bfnet-l-level"><?php esc_html_e( 'Level', 'bitforgenet-guest-tickets' ); ?></label>
					<select id="bfnet-l-level" name="level" onchange="this.form.submit()">
						<option value=""><?php esc_html_e( 'Everything', 'bitforgenet-guest-tickets' ); ?></option>
						<option value="info" <?php selected( $level, 'info' ); ?>><?php esc_html_e( 'Info', 'bitforgenet-guest-tickets' ); ?></option>
						<option value="warning" <?php selected( $level, 'warning' ); ?>><?php esc_html_e( 'Warning', 'bitforgenet-guest-tickets' ); ?></option>
						<option value="error" <?php selected( $level, 'error' ); ?>><?php esc_html_e( 'Error', 'bitforgenet-guest-tickets' ); ?></option>
						<option value="security" <?php selected( $level, 'security' ); ?>><?php esc_html_e( 'Security', 'bitforgenet-guest-tickets' ); ?></option>
					</select>
				</div>
			</form>
		</div>

		<div class="bfnet-panel__body bfnet-panel__body--flush">
			<?php if ( empty( $rows ) ) : ?>
				<div class="bfnet-empty-state"><p><?php esc_html_e( 'Nothing logged yet.', 'bitforgenet-guest-tickets' ); ?></p></div>
			<?php else : ?>
				<table class="bfnet-table">
					<thead>
						<tr>
							<th><?php esc_html_e( 'When', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Level', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Action', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Object', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'By', 'bitforgenet-guest-tickets' ); ?></th>
							<th><?php esc_html_e( 'Details', 'bitforgenet-guest-tickets' ); ?></th>
						</tr>
					</thead>
					<tbody>
					<?php foreach ( $rows as $row ) : ?>
						<?php
						$user    = $row->user_id ? get_userdata( (int) $row->user_id ) : null;
						$context = BFNET_Util::json( $row->context );
						?>
						<tr>
							<td>
								<?php echo esc_html( BFNET_Util::date( $row->created_at, 'j M H:i:s' ) ); ?>
								<div class="bfnet-table__sub"><?php echo esc_html( BFNET_Util::relative( $row->created_at ) ); ?></div>
							</td>
							<td>
								<span class="bfnet-badge" style="background:<?php echo esc_attr( isset( $colors[ $row->level ] ) ? $colors[ $row->level ] : '#5f6368' ); ?>">
									<?php echo esc_html( $row->level ); ?>
								</span>
							</td>
							<td class="bfnet-mono"><?php echo esc_html( $row->action ); ?></td>
							<td>
								<?php echo esc_html( $row->object_type ); ?>
								<?php if ( $row->object_id ) : ?>
									<span class="bfnet-table__sub">#<?php echo (int) $row->object_id; ?></span>
								<?php endif; ?>
							</td>
							<td><?php echo esc_html( $user ? $user->display_name : __( 'system', 'bitforgenet-guest-tickets' ) ); ?></td>
							<td style="max-width:340px">
								<?php if ( $context ) : ?>
									<span class="bfnet-mono" style="font-size:11.5px;word-break:break-word">
										<?php echo esc_html( BFNET_Util::excerpt( wp_json_encode( $context ), 160 ) ); ?>
									</span>
								<?php else : ?>
									—
								<?php endif; ?>
							</td>
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>

				<div class="bfnet-pager">
					<?php if ( $paged > 1 ) : ?>
						<a class="button button-small" href="<?php echo esc_url( add_query_arg( 'paged', $paged - 1 ) ); ?>">&laquo;</a>
					<?php endif; ?>
					<span><?php echo esc_html( sprintf( '%d', $paged ) ); ?></span>
					<?php if ( count( $rows ) === $per_page ) : ?>
						<a class="button button-small" href="<?php echo esc_url( add_query_arg( 'paged', $paged + 1 ) ); ?>">&raquo;</a>
					<?php endif; ?>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>
