<?php
/**
 * Admin page: Generation History.
 *
 * @package    100Prints
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

require_once HUNDRED_PRINTS_PATH . 'admin/partials/class-generation-history-list-table.php';

// Detail view.
if ( isset( $_GET['action'] ) && $_GET['action'] === 'view' && isset( $_GET['id'] ) ) {
	$service = new Hundred_Prints_Analytics_Service();
	$record  = $service->get_record( absint( $_GET['id'] ) );

	if ( ! $record ) {
		echo '<div class="wrap"><div class="notice notice-error"><p>Record not found.</p></div></div>';
		return;
	}

	$variables = json_decode( $record->variables_json, true );
	$user_display = 'Guest';
	if ( $record->user_id ) {
		$user = get_userdata( $record->user_id );
		$user_display = $user ? $user->display_name . ' (#' . $record->user_id . ')' : '#' . $record->user_id;
	}

	$back_url = admin_url( 'admin.php?page=100prints-generation-history' );
	?>
	<div class="wrap hundredp-wrap" style="background: #F8FAFC; min-height: 100vh; padding-bottom: 60px;">
		<?php
		$args = [ 'title' => 'Generation Detail' ];
		require HUNDRED_PRINTS_PATH . 'admin/partials/header.php';
		?>

		<div style="padding: 10px 32px; max-width: 900px; margin: 0 auto;">
			<p><a href="<?php echo esc_url( $back_url ); ?>" style="text-decoration:none;">&larr; Back to Generation History</a></p>

			<div style="background: #fff; border: 1px solid #E5E7EB; border-radius: 12px; padding: 32px; margin-top: 16px;">
				<h2 style="margin-top:0; font-size: 20px; font-weight: 700; color: #111827;">
					<?php echo esc_html( $record->template_name ?: $record->template_id ); ?>
				</h2>

				<table class="widefat" style="border: none; margin-top: 16px;">
					<tbody>
						<tr><th style="width:180px; padding:10px 12px; color:#6B7280;">Generation UUID</th><td style="padding:10px 12px;"><code style="font-size:12px;"><?php echo esc_html( $record->generation_uuid ); ?></code></td></tr>
						<tr><th style="padding:10px 12px; color:#6B7280;">Template ID</th><td style="padding:10px 12px;"><?php echo esc_html( $record->template_id ); ?></td></tr>
						<tr><th style="padding:10px 12px; color:#6B7280;">Output Format</th><td style="padding:10px 12px;"><?php echo esc_html( strtoupper( $record->output_format ) ); ?></td></tr>
						<tr><th style="padding:10px 12px; color:#6B7280;">Render Quality</th><td style="padding:10px 12px;"><?php echo esc_html( ucfirst( $record->render_quality ) ); ?></td></tr>
						<tr>
							<th style="padding:10px 12px; color:#6B7280;">Status</th>
							<td style="padding:10px 12px;">
								<?php
								$color = $record->status === 'success' ? '#16A34A' : '#DC2626';
								$bg    = $record->status === 'success' ? '#F0FDF4' : '#FEF2F2';
								?>
								<span style="color:<?php echo $color; ?>;background:<?php echo $bg; ?>;padding:2px 10px;border-radius:12px;font-size:12px;font-weight:500;">
									<?php echo esc_html( ucfirst( $record->status ) ); ?>
								</span>
							</td>
						</tr>
						<tr><th style="padding:10px 12px; color:#6B7280;">User</th><td style="padding:10px 12px;"><?php echo esc_html( $user_display ); ?></td></tr>
						<tr><th style="padding:10px 12px; color:#6B7280;">Visitor IP</th><td style="padding:10px 12px;"><?php echo esc_html( $record->visitor_ip ?: '—' ); ?></td></tr>
						<tr><th style="padding:10px 12px; color:#6B7280;">Page ID</th><td style="padding:10px 12px;"><?php echo absint( $record->page_id ); ?></td></tr>
						<tr><th style="padding:10px 12px; color:#6B7280;">Date</th><td style="padding:10px 12px;"><?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $record->created_at ) ) ); ?></td></tr>
					</tbody>
				</table>

				<?php if ( ! empty( $variables ) && is_array( $variables ) ) : ?>
					<h3 style="margin-top: 28px; font-size: 16px; font-weight: 600; color: #111827;">Resolved Variables</h3>
					<table class="widefat striped" style="margin-top: 12px;">
						<thead>
							<tr>
								<th style="font-weight:600; color:#374151;">Variable</th>
								<th style="font-weight:600; color:#374151;">Value</th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $variables as $key => $value ) : ?>
								<tr>
									<td><code style="font-size:12px;"><?php echo esc_html( $key ); ?></code></td>
									<td><?php echo esc_html( is_scalar( $value ) ? $value : wp_json_encode( $value ) ); ?></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				<?php else : ?>
					<p style="color: #9CA3AF; margin-top: 20px;">No variable data recorded for this generation.</p>
				<?php endif; ?>
			</div>
		</div>
	</div>
	<?php
	return;
}

// List view.
$list_table = new Hundred_Prints_Generation_History_List_Table();
$list_table->prepare_items();
?>

<div class="wrap hundredp-wrap" style="background: #F8FAFC; min-height: 100vh; padding-bottom: 60px;">
	<?php
	$args = [ 'title' => 'Generation History' ];
	require HUNDRED_PRINTS_PATH . 'admin/partials/header.php';
	?>

	<div style="padding: 10px 32px; max-width: 1200px; margin: 0 auto;">
		<form method="get">
			<input type="hidden" name="page" value="100prints-generation-history" />
			<?php
			$list_table->search_box( 'Search Generations', 'generation-search' );
			$list_table->display();
			?>
		</form>
	</div>
</div>
