<?php
/**
 * Adds a "Bookings" tab to the Classified Listing My Account dashboard.
 *
 * @package RadiusTheme\RadiusBooking\Integrations\ClassifiedListing
 */

namespace RadiusTheme\RadiusBooking\Integrations\ClassifiedListing;

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

/**
 * Class ListingDashboardHooks
 *
 * Registers a "Bookings" endpoint in the Classified Listing user dashboard
 * and renders the Radius Booking my-booking interface inside it.
 */
class ListingDashboardHooks {

	/**
	 * Endpoint slug for the bookings tab.
	 *
	 * @var string
	 */
	const ENDPOINT_SLUG = 'bookings';

	/**
	 * Constructor: register CL dashboard hooks.
	 */
	public function __construct() {
		// Register the rewrite endpoint directly. CL's Query::add_endpoints() runs
		// on `init` default priority and may fire before this class is constructed,
		// so our rtcl_my_account_endpoint filter would be missed for rewrite registration.
		// Calling add_rewrite_endpoint() here ensures WordPress knows this URL pattern.
		add_rewrite_endpoint( self::ENDPOINT_SLUG, EP_ROOT | EP_PAGES );

		add_filter( 'rtcl_my_account_endpoint', array( $this, 'addEndpoint' ) );
		add_filter( 'rtcl_account_menu_items', array( $this, 'addMenuItem' ) );
		add_action( 'rtcl_account_' . self::ENDPOINT_SLUG . '_endpoint', array( $this, 'renderEndpoint' ) );

		// Flush rewrite rules once when the endpoint is first registered.
		$this->maybeFlushRewriteRules();
	}

	/**
	 * Register the bookings endpoint with Classified Listing.
	 *
	 * @param array $endpoints Existing CL endpoints.
	 *
	 * @return array
	 */
	public function addEndpoint( $endpoints ) {
		$endpoints[ self::ENDPOINT_SLUG ] = self::ENDPOINT_SLUG;

		return $endpoints;
	}

	/**
	 * Add "Bookings" menu item to the CL dashboard navigation.
	 *
	 * @param array $items Existing menu items.
	 *
	 * @return array
	 */
	public function addMenuItem( $items ) {
		$position = array_search( 'listings', array_keys( $items ), true );
		if( !$position ) {
			$position = array_search( 'dashboard', array_keys( $items ), true );
		}
		$new_item = array( self::ENDPOINT_SLUG => __( 'Bookings', 'radius-booking' ) );

		if ( false !== $position ) {
			// Insert right after "listings".
			$before = array_slice( $items, 0, $position + 1, true );
			$after  = array_slice( $items, $position + 1, null, true );
			$items  = $before + $new_item + $after;
		} else {
			// Fallback: prepend.
			$items = $new_item + $items;
		}

		return $items;
	}

	/**
	 * Render the my-booking dashboard inside the CL endpoint content area.
	 */
	public function renderEndpoint() {
		if ( ! is_user_logged_in() ) {
			echo '<p>' . esc_html__( 'Please log in to view your bookings.', 'radius-booking' ) . '</p>';
			return;
		}

		echo '<div class="radius-booking rtrb-classified-listing">';
		rtrb_get_template(
			'my-booking/dashboard.php',
			array(
				'atts' => array( 'context' => 'classified_listing' ),
			)
		);
		echo '</div>';
	}

	/**
	 * Check if the current request is on the CL bookings endpoint.
	 *
	 * @return bool
	 */
	public static function isBookingsEndpoint() {
		global $wp;

		return isset( $wp->query_vars[ self::ENDPOINT_SLUG ] );
	}

	/**
	 * Flush rewrite rules once when the endpoint is first added.
	 */
	private function maybeFlushRewriteRules() {
		$option_key = 'rtrb_cl_bookings_endpoint_flushed';

		if ( get_option( $option_key ) ) {
			return;
		}

		flush_rewrite_rules();
		update_option( $option_key, true );
	}
}
