<?php
declare( strict_types=1 );

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

global $wpdb;

if (
	! empty( $_GET['fibofilters-action'] ) &&
	$_GET['fibofilters-action'] === 'fibofilters-debug-reset-settings' &&
	! empty( $_REQUEST['_wpnonce'] ) &&
	wp_verify_nonce( $_REQUEST['_wpnonce'], 'fibofilters_debug_maintenance' )
) {
	$fibofilters_table  = $wpdb->options;
	$fibofilters_column = 'option_name';

	if ( is_multisite() ) {
		$fibofilters_table  = $wpdb->sitemeta;
		$fibofilters_column = 'meta_key';
	}

	$wpdb->query(
		"
			DELETE
			FROM {$fibofilters_table}
			WHERE {$fibofilters_column} LIKE '%fibofilters_%'
		"
	);
	?>
	<script>
		window.fiboFiltersNotications.push({
			message: 'The options have been successfully removed.'
		});
	</script>
	<?php
}

if (
	! empty( $_GET['fibofilters-action'] ) &&
	$_GET['fibofilters-action'] === 'fibofilters-debug-reset-onboarding' &&
	! empty( $_REQUEST['_wpnonce'] ) &&
	wp_verify_nonce( $_REQUEST['_wpnonce'], 'fibofilters_debug_maintenance' )
) {
	delete_option( 'fibofilters_onboarding_finished' );
	?>
	<script>
		window.fiboFiltersNotications.push({
			message: 'The option has been reset.'
		});
	</script>
	<?php
}

if (
	! empty( $_GET['fibofilters-action'] ) &&
	$_GET['fibofilters-action'] === 'fibofilters-debug-reset-dismissed-warnings' &&
	! empty( $_REQUEST['_wpnonce'] ) &&
	wp_verify_nonce( $_REQUEST['_wpnonce'], 'fibofilters_debug_maintenance' )
) {
	delete_option( \FiboFilters\Config\Troubleshooting::DISMISSED_TESTS_OPTION_KEY );
	?>
	<script>
		window.fiboFiltersNotications.push({
			message: 'Dismissed warnings have been reset.'
		});
	</script>
	<?php
}
?>
<div class="components-card__body components-card-body fibofilters-settings-card">
	<div class="fibofilters-settings-header">
		<h2>Settings</h2>
		<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get" onsubmit="return confirm('Are you sure you want to reset the settings?');">
			<input type="hidden" name="page" value="fibofilters-debug">
			<input type="hidden" name="tab" value="maintenance">
			<input type="hidden" name="fibofilters-action" value="fibofilters-debug-reset-settings">
			<?php wp_nonce_field( 'fibofilters_debug_maintenance', '_wpnonce', false ); ?>
			<p>
				<input type="submit" class="components-button is-secondary is-destructive" value="Reset plugin settings">
			</p>
		</form>

		<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get">
			<input type="hidden" name="page" value="fibofilters-debug">
			<input type="hidden" name="tab" value="maintenance">
			<input type="hidden" name="fibofilters-action" value="fibofilters-debug-reset-onboarding">
			<?php wp_nonce_field( 'fibofilters_debug_maintenance', '_wpnonce', false ); ?>
			<p>
				<input type="submit" class="components-button is-secondary" value="Show onboarding again on plugin settings page">
			</p>
		</form>
	</div>

	<div class="fibofilters-settings-header">
		<h2>Troubleshooting</h2>
		<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get">
			<input type="hidden" name="page" value="fibofilters-debug">
			<input type="hidden" name="tab" value="maintenance">
			<input type="hidden" name="fibofilters-action" value="fibofilters-debug-reset-dismissed-warnings">
			<?php wp_nonce_field( 'fibofilters_debug_maintenance', '_wpnonce', false ); ?>
			<p>
				<input type="submit" class="components-button is-secondary is-destructive" value="Reset dismissed warnings">
			</p>
		</form>
	</div>
</div>
