<?php
/**
 * Builds the `BreadcrumbList` schema piece for the current page.
 *
 * Yoast emits its own breadcrumbs (we skip in Yoast mode); this piece is
 * for standalone mode + AEO-mode delegation. Tracks Home → Inventory →
 * (Make? → Model?) → Vehicle for vehicle pages, Home → Dealers → Dealer
 * for dealer pages.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Integrations\Schema;

use ThemeREX\AutoDealer\PostTypes\DealerPostType;
use ThemeREX\AutoDealer\PostTypes\VehiclePostType;

class BreadcrumbSchema {

	/**
	 * @return array<string, mixed>
	 */
	public function buildPiece(): array {
		$crumbs = array(
			array(
				'name' => __( 'Home', 'trx-auto-dealer' ),
				'url'  => home_url( '/' ),
			),
		);

		if ( is_singular( VehiclePostType::POST_TYPE ) ) {
			$archive_url = get_post_type_archive_link( VehiclePostType::POST_TYPE );
			if ( $archive_url ) {
				$crumbs[] = array(
					'name' => __( 'Inventory', 'trx-auto-dealer' ),
					'url'  => $archive_url,
				);
			}
			$post_id = (int) get_queried_object_id();

			$make_terms = get_the_terms( $post_id, 'trx_ad_make' );
			if ( ! is_wp_error( $make_terms ) && ! empty( $make_terms ) ) {
				$make_url = get_term_link( $make_terms[0] );
				$crumbs[] = array(
					'name' => $make_terms[0]->name,
					'url'  => is_wp_error( $make_url ) ? '' : $make_url,
				);
			}
			$crumbs[] = array(
				'name' => (string) get_the_title( $post_id ),
				'url'  => (string) get_permalink( $post_id ),
			);
		} elseif ( is_singular( DealerPostType::POST_TYPE ) ) {
			$archive_url = get_post_type_archive_link( DealerPostType::POST_TYPE );
			if ( $archive_url ) {
				$crumbs[] = array(
					'name' => __( 'Dealers', 'trx-auto-dealer' ),
					'url'  => $archive_url,
				);
			}
			$dealer_id = (int) get_queried_object_id();
			$crumbs[]  = array(
				'name' => (string) get_the_title( $dealer_id ),
				'url'  => (string) get_permalink( $dealer_id ),
			);
		} elseif ( is_post_type_archive( VehiclePostType::POST_TYPE ) ) {
			$crumbs[] = array(
				'name' => post_type_archive_title( '', false ),
				'url'  => (string) get_post_type_archive_link( VehiclePostType::POST_TYPE ),
			);
		} elseif ( is_tax() ) {
			$term = get_queried_object();
			if ( $term instanceof \WP_Term ) {
				$archive_url = get_post_type_archive_link( VehiclePostType::POST_TYPE );
				if ( $archive_url ) {
					$crumbs[] = array(
						'name' => __( 'Inventory', 'trx-auto-dealer' ),
						'url'  => $archive_url,
					);
				}
				$term_url = get_term_link( $term );
				$crumbs[] = array(
					'name' => $term->name,
					'url'  => is_wp_error( $term_url ) ? '' : $term_url,
				);
			}
		}

		// Single-crumb breadcrumb (Home only) is useless — skip.
		if ( count( $crumbs ) < 2 ) {
			return array();
		}

		$items = array();
		foreach ( $crumbs as $i => $crumb ) {
			$items[] = array(
				'@type'    => 'ListItem',
				'position' => $i + 1,
				'name'     => (string) $crumb['name'],
				'item'     => (string) $crumb['url'],
			);
		}

		// Current request URL for the @id. `add_query_arg( array() )` returns the
		// current REQUEST_URI without modification — the array form avoids the
		// PHP 8.1 "null used as array offset" deprecation that `add_query_arg(
		// null, null )` triggers in core's add_query_arg().
		$current_url = home_url( add_query_arg( array() ) );

		$piece = array(
			'@type'           => 'BreadcrumbList',
			'@id'             => trailingslashit( (string) ( $current_url ?: home_url( '/' ) ) ) . '#breadcrumb',
			'itemListElement' => $items,
		);

		/**
		 * Filter the BreadcrumbList piece.
		 *
		 * @param array $piece
		 */
		return (array) apply_filters( 'trx_ad_schema_breadcrumb_piece', $piece );
	}
}
