<?php
/**
 * Product Catalog Admin Page
 *
 * @package MindWebConcierge
 */

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

add_action( 'admin_enqueue_scripts', 'mindweco_product_catalog_enqueue_scripts' );

function mindweco_product_catalog_enqueue_scripts( $hook_suffix ) {

	if ( 'mind-web_page_mind-web-product-catalog' !== $hook_suffix ) {
		return;
	}

	$plugin_url = plugin_dir_url( __DIR__ );
	$ver        = defined( 'MIND_WEB_VERSION' ) ? MIND_WEB_VERSION : '1.3.0';

	wp_enqueue_style(
		'mindweco-product-catalog-style',
		$plugin_url . 'assets/css/mindweco_product_catalog.css',
		array(),
		$ver
	);

	wp_enqueue_script(
		'mindweco-toast',
		$plugin_url . 'assets/js/components/Toast.js',
		array( 'jquery' ),
		$ver,
		true
	);

	wp_enqueue_script(
		'mindweco-modal',
		$plugin_url . 'assets/js/components/Modal.js',
		array( 'jquery' ),
		$ver,
		true
	);

	wp_enqueue_script(
		'mindweco-product-catalog-js',
		$plugin_url . 'assets/js/mindweco_product_catalog.js',
		array( 'jquery', 'mindweco-toast', 'mindweco-modal' ),
		$ver,
		true
	);

	wp_localize_script(
		'mindweco-product-catalog-js',
		'mindwecoProductCatalog',
		array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'mindweco_product_catalog_nonce' ),
		)
	);
}

function mindweco_product_catalog_page() {

	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( esc_html__( 'You do not have permission to access this page.', 'mind-web-concierge' ) );
	}

	global $wpdb;

	$posts_table   = $wpdb->prefix . 'posts';
	$indexed_table = $wpdb->prefix . 'mindweco_indexed_products';

	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
	$total_products = (int) $wpdb->get_var(
		$wpdb->prepare(
			"SELECT COUNT(*) FROM `{$posts_table}` WHERE post_type = %s AND post_status = %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			'product',
			'publish'
		)
	);

	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
	$indexed_count = (int) $wpdb->get_var(
		$wpdb->prepare(
			"SELECT COUNT(*) FROM `{$indexed_table}` AS i
			INNER JOIN `{$posts_table}` AS p ON i.post_id = p.ID
			WHERE p.post_type = %s AND p.post_status = %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			'product',
			'publish'
		)
	);

	$pending_indexing = max( 0, $total_products - $indexed_count );
	$progress         = $total_products > 0
		? round( ( $indexed_count / $total_products ) * 100, 2 )
		: 0;

	?>
	<div id="mind-web-settings-wrapper">
		<div class="wrap">

			<div class="mindweco-card">
				<h4 class="product-catalog-title">
					<i class="bi bi-sliders" aria-hidden="true"></i>
					<?php esc_html_e( 'WooCommerce Product Settings', 'mind-web-concierge' ); ?>
				</h4>
			</div>

			<h2 class="nav-tab-wrapper mindweco-nav-tabs">
				<a href="#indexing-product" class="nav-tab mindweco-nav-tab nav-tab-active" data-tab="indexing-product">
					<i class="bi bi-lightning-charge-fill" aria-hidden="true"></i>
					<?php esc_html_e( 'Indexing Products', 'mind-web-concierge' ); ?>
				</a>
				<a href="#manage-product" class="nav-tab mindweco-nav-tab" data-tab="manage-product">
					<i class="bi bi-list-check" aria-hidden="true"></i>
					<?php esc_html_e( 'Manage Product Catalog', 'mind-web-concierge' ); ?>
				</a>
			</h2>

			<div id="tab-indexing-product" class="tab-content" style="display:block;">
				<div class="bg-white p-4 rounded shadow-sm">
					<div class="indexing-status">
						<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:15px;">
							<div>
								<div class="indexing-elements-progress"><?php esc_html_e( 'Total Products', 'mind-web-concierge' ); ?></div>
								<div style="font-size:24px;font-weight:700;color:#1a365d;"><?php echo esc_html( $total_products ); ?></div>
							</div>
							<div>
								<div class="indexing-elements-progress"><?php esc_html_e( 'Not Indexed', 'mind-web-concierge' ); ?></div>
								<div id="to-index-count-products" style="font-size:24px;font-weight:700;color:#1a365d;"><?php echo esc_html( $pending_indexing ); ?></div>
							</div>
							<div>
								<div class="indexing-elements-progress"><?php esc_html_e( 'Indexed', 'mind-web-concierge' ); ?></div>
								<div id="indexed-count-products" style="font-size:24px;font-weight:700;color:#10b981;"><?php echo esc_html( $indexed_count ); ?></div>
							</div>
							<div>
								<div class="indexing-elements-progress"><?php esc_html_e( 'Progress', 'mind-web-concierge' ); ?></div>
								<div id="index-progress-products" style="font-size:24px;font-weight:700;color:#1a365d;"><?php echo esc_html( $progress ); ?>%</div>
							</div>
						</div>
					</div>

					<button id="mindweco-index-all-products" class="button">
						<i class="bi bi-play-circle-fill" aria-hidden="true" style="font-size:16px;"></i>
						<span><?php esc_html_e( 'Start Indexing', 'mind-web-concierge' ); ?></span>
					</button>

					<div id="progress-container">
						<div id="progress-wrapper">
							<div id="progress-bar">
								<span id="progress-text">0%</span>
							</div>
						</div>
						<div id="progress-status"></div>
					</div>

					<div id="mindweco-index-all-result" style="margin-top:20px;"></div>
				</div>
			</div>

			<div id="tab-manage-product" class="tab-content" style="display:none;">
				<div class="bg-white p-4 rounded shadow-sm">
					<div class="wrap">
						<div class="mindweco-header-wrap d-flex align-items-center border-bottom pb-3 mb-3">
							<div class="flex-grow-1">
								<h1 class="fw-semibold m-0">
									<i class="bi bi-list-check me-2 text-primary" aria-hidden="true"></i>
									<?php esc_html_e( 'All Indexed Products', 'mind-web-concierge' ); ?>
								</h1>
								<p class="text-secondary fw-bold m-0 mt-1">
									<?php esc_html_e( 'A complete list of all successfully indexed products.', 'mind-web-concierge' ); ?>
								</p>
							</div>

							<div class="search-wrapper flex-shrink-0 mx-4 position-relative" style="width:450px;">
								<i class="bi bi-search position-absolute text-muted search-icon-product-catalog" aria-hidden="true" ></i>
								<label for="search-input" class="screen-reader-text">
									<?php esc_html_e( 'Search indexed products', 'mind-web-concierge' ); ?>
								</label>
								<input
									type="text"
									class="form-control ps-5"
									id="search-input"
									placeholder="<?php esc_attr_e( 'Search indexed products...', 'mind-web-concierge' ); ?>"
									aria-label="<?php esc_attr_e( 'Search indexed products', 'mind-web-concierge' ); ?>"
								/>
							</div>

							<div class="flex-grow-1 d-flex justify-content-end gap-2">
								<button
									id="mindweco-unindex-all-products"
									class="btn btn-danger d-flex align-items-center gap-2 px-3"
									aria-label="<?php esc_attr_e( 'Unindex all products', 'mind-web-concierge' ); ?>"
								>
									<i class="bi bi-trash-fill" aria-hidden="true"></i>
									<span><?php esc_html_e( 'Unindex All', 'mind-web-concierge' ); ?></span>
								</button>
								<button
									class="btn btn-success btn-index-items d-flex align-items-center gap-2 px-3"
									aria-label="<?php esc_attr_e( 'Index new products', 'mind-web-concierge' ); ?>"
								>
									<i class="bi bi-check-circle-fill" aria-hidden="true"></i>
									<span><?php esc_html_e( 'Index Items', 'mind-web-concierge' ); ?></span>
								</button>
							</div>
						</div>

						<div id="mindweco-unindex-result" class="mb-3"></div>

						<div class="table-responsive border custom-rounded" style="border-color:#e5e7eb;">
							<table id="table-indexed-products" class="wp-list-table widefat fixed striped m-0">
								<thead>
									<tr>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'ID', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Item ID', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Product ID', 'mind-web-concierge' ); ?></th>						
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Category', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Image', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Name', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Regular Price', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Sale Price', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Author', 'mind-web-concierge' ); ?></th>
										<th class="table-indexed-title" scope="col"><?php esc_html_e( 'Actions', 'mind-web-concierge' ); ?></th>
									</tr>
								</thead>
								<tbody></tbody>
							</table>
						</div>

						<div id="pagination"></div>
					</div>
				</div>
			</div>

		</div>
	</div>
	<?php
}