<?php
/**
 * Autopilot — Logs.
 * @var \CartCue\Plugin $plugin
 * @package CartCue
 */
defined( 'ABSPATH' ) || exit;

/* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound */

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- admin log filter UI.
$filter_status = isset( $_GET['status'] ) ? sanitize_key( wp_unslash( $_GET['status'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- admin log pagination.
$current_page  = isset( $_GET['paged'] ) ? max( 1, absint( wp_unslash( $_GET['paged'] ) ) ) : 1;
$per_page      = 50;
$offset        = ( $current_page - 1 ) * $per_page;

$args  = [ 'status' => $filter_status, 'limit' => $per_page, 'offset' => $offset ];
$logs  = $plugin->logger->get_logs( $args );
$total = $plugin->logger->count_logs( $args );
$pages = (int) ceil( $total / $per_page );
?>
<div class="ap-wrap">
	<div class="ap-page-header">
		<div>
			<h1 class="ap-page-title"><?php esc_html_e( 'Logs', 'cartcue-sales-automation' ); ?></h1>
			<p class="ap-page-sub"><?php esc_html_e( 'Every time an automation runs, it leaves a record here.', 'cartcue-sales-automation' ); ?></p>
		</div>
	</div>
	<ul class="subsubsub">
		<?php foreach ( [ '' => __( 'All', 'cartcue-sales-automation' ), 'success' => __( 'Success', 'cartcue-sales-automation' ), 'skipped' => __( 'Skipped', 'cartcue-sales-automation' ), 'error' => __( 'Error', 'cartcue-sales-automation' ) ] as $sk => $sl ) :
			$url = add_query_arg( [ 'page' => 'cartcue-logs', 'status' => $sk ], admin_url( 'admin.php' ) );
			printf( '<li><a href="%s"%s>%s</a> | </li>', esc_url( $url ), $filter_status === $sk ? ' class="current"' : '', esc_html( $sl ) );
		endforeach; ?>
	</ul>
	<table class="wp-list-table widefat fixed striped" style="margin-top:10px">
		<thead>
			<tr>
				<th><?php esc_html_e( 'Date', 'cartcue-sales-automation' ); ?></th>
				<th><?php esc_html_e( 'Automation', 'cartcue-sales-automation' ); ?></th>
				<th><?php esc_html_e( 'Trigger', 'cartcue-sales-automation' ); ?></th>
				<th><?php esc_html_e( 'Customer', 'cartcue-sales-automation' ); ?></th>
				<th><?php esc_html_e( 'Result', 'cartcue-sales-automation' ); ?></th>
				<th><?php esc_html_e( 'Detail', 'cartcue-sales-automation' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( empty( $logs ) ) : ?>
			<tr><td colspan="6" style="text-align:center;padding:30px;color:#888"><?php esc_html_e( 'No log entries yet.', 'cartcue-sales-automation' ); ?></td></tr>
			<?php else : foreach ( $logs as $log ) :
				$trigger = $plugin->get_trigger( $log['trigger_slug'] ); ?>
			<tr>
				<td><?php echo esc_html( mysql2date( get_option( 'date_format' ) . ' H:i', $log['created_at'] ) ); ?></td>
				<td><?php echo esc_html( $log['workflow_name'] ); ?></td>
				<td><?php echo $trigger ? esc_html( $trigger->get_label() ) : esc_html( $log['trigger_slug'] ); ?></td>
				<td><?php echo esc_html( $log['customer_email'] ); ?></td>
				<td><span class="ap-badge ap-badge--<?php echo esc_attr( $log['status'] ); ?>"><?php echo esc_html( ucfirst( $log['status'] ) ); ?></span></td>
				<td style="font-size:12px;color:#666"><?php echo esc_html( $log['message'] ); ?></td>
			</tr>
			<?php endforeach; endif; ?>
		</tbody>
	</table>
	<?php
	if ( $pages > 1 ) {
		echo wp_kses_post(
			paginate_links(
				[
					'base'    => add_query_arg( 'paged', '%#%' ),
					'format'  => '',
					'current' => $current_page,
					'total'   => $pages,
				]
			)
		);
	}
	?>
</div>
<?php
/* phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound */
?>
