<?php
declare( strict_types=1 );

use FiboFilters\Config\Index;
use FiboFilters\DI\ContainerWrapper;

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

function fibofilters_is_debug_action( string $action ): bool {
	return ! empty( $_GET['fibofilters-action'] ) &&
		   $_GET['fibofilters-action'] === $action &&
		   ! empty( $_REQUEST['_wpnonce'] ) &&
		   wp_verify_nonce( $_REQUEST['_wpnonce'], 'fibofilters_debug_indexer' );
}

if (
	! empty( $_GET['fibofilters-action'] ) &&
	$_GET['fibofilters-action'] === 'fibofilters-debug-delete-db-tables' &&
	! empty( $_REQUEST['_wpnonce'] ) &&
	wp_verify_nonce( $_REQUEST['_wpnonce'], 'fibofilters_debug_indexer' )
) {
	/** @var Index $fibofilters_index */
	$fibofilters_index = ContainerWrapper::get_instance()->get( FiboFilters\Config\Index::class );
	$fibofilters_index->remove_index();
	?>
	<script>
		window.fiboFiltersNotications.push({
			message: 'The index tables have been deleted.'
		});
	</script>
	<?php
}

?>
<div class="components-card__body components-card-body fibofilters-settings-card">
	<div class="fibofilters-settings-header">
		<h2>Maintenance</h2>
		<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="get" onsubmit="return confirm('Are you sure?');">
			<input type="hidden" name="page" value="fibofilters-debug">
			<input type="hidden" name="tab" value="indexer">
			<input type="hidden" name="fibofilters-action" value="fibofilters-debug-delete-db-tables">
			<?php wp_nonce_field( 'fibofilters_debug_indexer', '_wpnonce', false ); ?>
			<p>
				<input type="submit" class="components-button is-secondary is-destructive" value="Delete indexer tables">
			</p>
		</form>

		<br/>
		<h2>Product data for indexing</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="indexer">
			<input type="hidden" name="fibofilters-action" value="fibofilters-debug-get-source-query-data">
			<?php wp_nonce_field( 'fibofilters_debug_indexer', '_wpnonce', false ); ?>
			<p style="display: flex; gap: 8px;">
				<input type="text" name="fibofilters-product-id" style="min-height: 36px" placeholder="Product or variation ID" value="<?php echo esc_attr( $_GET['fibofilters-product-id'] ?? '' ); ?>">
				<input type="submit" class="components-button is-secondary" value="Retrieve product data for indexing">
			</p>
			<?php
			if (
				fibofilters_is_debug_action( 'fibofilters-debug-get-source-query-data' ) &&
				! empty( $_REQUEST['fibofilters-product-id'] ) &&
				intval( $_REQUEST['fibofilters-product-id'] ) > 0
			) {
				/** @var FiboFilters\Indexer\SourceQueryFactory $fibofilters_source_query_factory */
				$fibofilters_source_query_factory = ContainerWrapper::get_instance()->get( FiboFilters\Indexer\SourceQueryFactory::class );
				$fibofilters_source_query         = $fibofilters_source_query_factory->get(
					'filters',
					[
						'post_in' => [ (int) $_REQUEST['fibofilters-product-id'] ],
						'context' => 'indexer',
					]
				);

				$fibofilters_data = $fibofilters_source_query->get_data();

				if ( empty( $fibofilters_data ) ) {
					?>
					<div class="components-notice is-warning" style="margin-top: 1em;">
						<div class="components-notice__content">
							No data
						</div>
					</div>
					<?php
				} else {
					?>
					<pre class="fibofilters-pre"><?php echo var_export( $fibofilters_data ); ?></pre>
					<?php
				}
			}
			?>
		</form>
	</div>
</div>
