<?php

// If this file is called directly, abort.
if (! defined('ABSPATH')) {
	exit;
}

class AthenaSearchResultsShortcode {
	public function __construct() {
		add_shortcode('athena_search_results', [$this, 'renderAthenaSearchResults']);
	}

	public function enqueue_assets() {
		if (!wp_script_is('athena-x-ai-index', 'enqueued')) {
			wp_enqueue_script('athena-x-ai-index', ASWP_PLUGIN_URL . 'public/js/athena-x-ai-index.js', [], '1.0.0', true);

			// Localize config
			$options = get_option('athena_options', []);
			$apiUrl = rtrim($options['athena_dashboard_url'] ?? '', '/') . '/';
			$websiteToken = $options['dashboard_website_token'] ?? '';
			$page_id = intval($options['athena_landing_page'] ?? 0);
			$full_url = $page_id ? get_permalink($page_id) : home_url('/');
			$form_url = untrailingslashit(wp_make_link_relative($full_url)) ?: '/';

			wp_localize_script('athena-x-ai-index', 'AthenaConfig', [
				'apiUrl' => $apiUrl,
				'originUrl' => get_site_url() . '/',
				'websiteToken' => $websiteToken,
				'formUrl' => $form_url,
			]);
		}

		wp_enqueue_style('autocomplete-css', ASWP_PLUGIN_URL . 'public/css/autocomplete.css', [], '1.0.0');
		wp_enqueue_style('cropper-css', ASWP_PLUGIN_URL . 'public/css/cropper.css', [], '1.0.0');
		wp_enqueue_style('search-athena-landing-css', ASWP_PLUGIN_URL . 'public/css/search-athena-landing.css', [], '1.0.0');
	}

	public function renderAthenaSearchResults($atts) {
		$this->enqueue_assets();

		return '
			<div class="athena-landing-page__container" id="athena-landing-page__container">

				<div class="breadcrumbs-container"></div>
				<div class="category-name-container"></div>

				<!-- Visual Filter Container -->
				<div class="visual-filters__container" id="visual-filters__container">
					<div>
						<h3 class="visual-filters__heading" id="visual-filter__heading"></h3>
					</div>
					<div id="visual-filters__inner-container" class="visual-filters__inner-container">
						<div id="visual-filters" class="visual-filters"></div>
					</div>
				</div>
				<!-- Visual Filter Container -->

				<div class="athena-landing-container" id="athena-main-container">
					<div id="athena-landing-sidebar" class="athena-sidebar"></div>

					<div id="athena-landing-content" class="athena-product-content">
						<div class="filter-opener-container hide-icon">
							<span class="filter-opener">Filter</span>
						</div>
						<div class="athena-search-banner"></div>

						<div class="search-title hide-title">
							<span>Search results for: </span>
						</div>
						<div class="athena-search-active-filters-container">
							<p class="active-filters-title hide-filter-title"></p>
							<div class="athena-search-active-filters"></div>
						</div>

						<div class="athena-search-toolbar">

							<div class="athena-search-toolbar-sorter"></div>
							<div class="athena-search-products-per-page"></div>
							<div class="athena-search-amount"></div>
							<div class="athena-search-view"></div>
						</div>
						<div class="athena-search-banner"></div>
						<div class="athena-search-load-prev hide-btn"><button>Load Previous products</button></div>
						<div class="products wrapper">
							<div class="best-matches-div">
								<p>Best Matches</p>
							</div>
							<ul class="products list items product-items"></ul>
							<div class="also-relevant-div">
								<p>Also Relevant</p>
							</div>
							<ul class="products list items product-items product-items-cutoff"></ul>
						</div>
						<div class="athena-search-pagination-container"></div>
					</div>
				</div>
				<footer class="athena-search-footer" id="athena-search-footer"></footer>
			</div>
		';
	}
}