<?php
/**
 * Plain-permalink archive router.
 *
 * Post-type archives are reachable by their pretty path (e.g. `/inventory/`)
 * only when WordPress' rewrite rules are active — i.e. when a non-empty
 * permalink structure is configured. Under the default **Plain** permalinks
 * (`?p=123`) no rewrite rules exist, so a visit to `/inventory/` matches
 * nothing and WordPress falls through to the site's front page (the blog
 * feed). Testers hit this on a fresh install that still had Plain permalinks.
 *
 * WordPress already serves the archive at the query-arg form
 * `?post_type=trx_ad_listing` under Plain permalinks. This router rescues the
 * pretty path too: when permalinks are Plain and the requested path equals the
 * configured archive slug, it injects the `post_type` query var — exactly the
 * state `?post_type=trx_ad_listing` would produce — so `is_post_type_archive()`
 * becomes true, the template loader serves `archive-vehicle.php`, and the
 * filter sidebar works. No redirect: the URL stays `/inventory/`.
 *
 * @package ThemeREX\AutoDealer
 * @version 1.0.3
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Frontend;

defined( 'ABSPATH' ) || exit;
use ThemeREX\AutoDealer\PostTypes\DealerPostType;
use ThemeREX\AutoDealer\PostTypes\VehiclePostType;

/**
 * Routes the pretty archive path to the post-type archive query when pretty
 * permalinks are disabled.
 */
class PlainPermalinkArchive {

	/**
	 * Register hooks.
	 *
	 * @return void
	 */
	public function register(): void {
		add_action( 'parse_request', array( $this, 'routeArchive' ) );
	}

	/**
	 * Inject the post-type-archive query var for `/inventory/` (and the
	 * configured dealer archive slug) when pretty permalinks are off.
	 *
	 * @param \WP $wp Current WordPress environment instance (passed by ref by core).
	 * @return void
	 */
	public function routeArchive( \WP $wp ): void {
		// Pretty permalinks resolve the path natively — nothing to rescue.
		if ( '' !== (string) get_option( 'permalink_structure', '' ) ) {
			return;
		}

		// An explicit query (?post_type=, ?p=, ?page_id=, feeds, REST, etc.)
		// already routed the request — don't override it.
		if ( ! empty( $wp->query_vars['post_type'] )
			|| ! empty( $wp->query_vars['p'] )
			|| ! empty( $wp->query_vars['page_id'] )
			|| ! empty( $wp->query_vars['name'] )
			|| ! empty( $wp->query_vars['pagename'] ) ) {
			return;
		}

		$path = trim( (string) $wp->request, '/' );
		if ( '' === $path ) {
			return;
		}

		$archive_slug = (string) get_option( 'trx_ad_archive_slug', 'inventory' );
		if ( '' === $archive_slug ) {
			$archive_slug = 'inventory';
		}

		if ( $path === $archive_slug ) {
			// Equivalent to visiting `?post_type=trx_ad_listing`.
			$wp->query_vars['post_type'] = VehiclePostType::POST_TYPE;
			return;
		}

		// Dealer archive slug is the fixed `dealers` rewrite slug registered
		// by DealerPostType (not user-configurable).
		if ( 'dealers' === $path ) {
			$wp->query_vars['post_type'] = DealerPostType::POST_TYPE;
		}
	}
}
