<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! current_user_can( 'manage_options' ) ) {
	return;
}

$smc_logger = Smart_Media_Cleanup\Plugin::instance()->get_logger();

// Handle clear log action.
if ( isset( $_POST['smc_clear_log'] ) && check_admin_referer( 'smc_clear_log' ) ) {
	$smc_logger->clear();
	echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Log cleared.', 'smart-media-cleanup' ) . '</p></div>';
}

$smc_current_page = max( 1, absint( $_GET['paged'] ?? 1 ) );
$smc_event_filter = sanitize_key( wp_unslash( $_GET['event_type'] ?? '' ) );
$smc_date_from    = sanitize_text_field( wp_unslash( $_GET['date_from'] ?? '' ) );
$smc_date_to      = sanitize_text_field( wp_unslash( $_GET['date_to'] ?? '' ) );

$smc_per_page = 50;
$smc_entries  = $smc_logger->get_entries( array(
	'per_page'   => $smc_per_page,
	'page'       => $smc_current_page,
	'event_type' => $smc_event_filter,
	'date_from'  => $smc_date_from,
	'date_to'    => $smc_date_to,
) );
$smc_total    = $smc_logger->count_entries();
?>
<div class="wrap smc-wrap">
	<h1><?php esc_html_e( 'Smart Media Cleanup — Log', 'smart-media-cleanup' ); ?></h1>

	<form method="get" class="smc-log-filters">
		<input type="hidden" name="page" value="smc-log">
		<select name="event_type">
			<option value=""><?php esc_html_e( 'All events', 'smart-media-cleanup' ); ?></option>
			<option value="delete_on_post" <?php selected( $smc_event_filter, 'delete_on_post' ); ?>><?php esc_html_e( 'Deleted on post', 'smart-media-cleanup' ); ?></option>
			<option value="orphan_delete" <?php selected( $smc_event_filter, 'orphan_delete' ); ?>><?php esc_html_e( 'Orphan delete', 'smart-media-cleanup' ); ?></option>
			<option value="error" <?php selected( $smc_event_filter, 'error' ); ?>><?php esc_html_e( 'Error', 'smart-media-cleanup' ); ?></option>
		</select>
		<input type="date" name="date_from" value="<?php echo esc_attr( $smc_date_from ); ?>" placeholder="<?php esc_attr_e( 'From', 'smart-media-cleanup' ); ?>">
		<input type="date" name="date_to" value="<?php echo esc_attr( $smc_date_to ); ?>" placeholder="<?php esc_attr_e( 'To', 'smart-media-cleanup' ); ?>">
		<?php submit_button( esc_html__( 'Filter', 'smart-media-cleanup' ), 'secondary', '', false ); ?>
	</form>

	<?php if ( empty( $smc_entries ) ) : ?>
		<p><?php esc_html_e( 'No log entries found.', 'smart-media-cleanup' ); ?></p>
	<?php else : ?>
		<table class="wp-list-table widefat fixed striped smc-log-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Date', 'smart-media-cleanup' ); ?></th>
					<th><?php esc_html_e( 'Event', 'smart-media-cleanup' ); ?></th>
					<th><?php esc_html_e( 'Attachment ID', 'smart-media-cleanup' ); ?></th>
					<th><?php esc_html_e( 'Post ID', 'smart-media-cleanup' ); ?></th>
					<th><?php esc_html_e( 'File', 'smart-media-cleanup' ); ?></th>
					<th><?php esc_html_e( 'Message', 'smart-media-cleanup' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $smc_entries as $smc_entry ) : ?>
				<tr>
					<td><?php echo esc_html( $smc_entry->created_at ); ?></td>
					<td><code><?php echo esc_html( $smc_entry->event_type ); ?></code></td>
					<td><?php echo esc_html( $smc_entry->attachment_id ); ?></td>
					<td><?php echo $smc_entry->post_id ? esc_html( $smc_entry->post_id ) : '—'; ?></td>
					<td class="smc-url-cell"><?php echo esc_html( basename( $smc_entry->attachment_url ?? '' ) ); ?></td>
					<td><?php echo esc_html( $smc_entry->message ); ?></td>
				</tr>
				<?php endforeach; ?>
			</tbody>
		</table>

		<?php
		$smc_total_pages = (int) ceil( $smc_total / $smc_per_page );
		if ( $smc_total_pages > 1 ) {
			echo wp_kses_post( paginate_links( array(
				'base'      => add_query_arg( 'paged', '%#%' ),
				'format'    => '',
				'current'   => $smc_current_page,
				'total'     => $smc_total_pages,
				'prev_text' => '&laquo;',
				'next_text' => '&raquo;',
			) ) );
		}
		?>
	<?php endif; ?>

	<div class="smc-notice smc-notice--info" style="margin-top:20px">
		<span class="dashicons dashicons-info"></span>
		<div>
			<?php esc_html_e( 'The log is for your reference only — clearing it does not affect your media files.', 'smart-media-cleanup' ); ?>
		</div>
	</div>

	<form method="post" class="smc-clear-log-form" style="margin-top:12px">
		<?php wp_nonce_field( 'smc_clear_log' ); ?>
		<?php submit_button( esc_html__( 'Clear All Log Entries', 'smart-media-cleanup' ), 'delete', 'smc_clear_log', false, array( 'onclick' => 'return confirm("' . esc_js( __( 'Are you sure? All log entries will be permanently removed.', 'smart-media-cleanup' ) ) . '")' ) ); ?>
	</form>
</div>
