<?php
/**
 * Breadcrumb schema type
 *
 * Outputs schema.org/BreadcrumbList markup for site navigation.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Schema_Breadcrumb extends BW_Schema_Schema_Base {

	/**
	 * Schema type
	 *
	 * @var string
	 */
	protected $type = 'BreadcrumbList';

	/**
	 * Build the breadcrumb schema
	 *
	 * @return void
	 */
	protected function build() {
		$breadcrumbs = $this->get_breadcrumb_trail();

		if ( empty( $breadcrumbs ) ) {
			return;
		}

		$items = array();
		$position = 1;

		foreach ( $breadcrumbs as $breadcrumb ) {
			$items[] = array(
				'@type'    => 'ListItem',
				'position' => $position,
				'name'     => $breadcrumb['name'],
				'item'     => $breadcrumb['url'],
			);
			$position++;
		}

		$this->set_nested( 'itemListElement', $items );
	}

	/**
	 * Get breadcrumb trail for current page
	 *
	 * @return array Array of breadcrumb items (name, url pairs)
	 */
	private function get_breadcrumb_trail() {
		$breadcrumbs = array();

		// Home link (always first)
		$breadcrumbs[] = array(
			'name' => __( 'Home', 'bw-schema' ),
			'url'  => home_url(),
		);

		// Front page (no breadcrumb needed)
		if ( is_front_page() ) {
			return $breadcrumbs;
		}

		// Blog home
		if ( is_home() ) {
			$breadcrumbs[] = array(
				'name' => get_the_title( get_option( 'page_for_posts' ) ),
				'url'  => get_permalink( get_option( 'page_for_posts' ) ),
			);
			return $breadcrumbs;
		}

		// Singular post/page
		if ( is_singular() ) {
			global $post;

			// Parent pages
			if ( 'page' === $post->post_type && $post->post_parent ) {
				$parent_pages = array();
				$current_page = $post->post_parent;

				while ( $current_page ) {
					$page = get_post( $current_page );
					$parent_pages[] = $page;
					$current_page = $page->post_parent;
				}

				// Add in reverse order
				foreach ( array_reverse( $parent_pages ) as $parent ) {
					$breadcrumbs[] = array(
						'name' => get_the_title( $parent->ID ),
						'url'  => get_permalink( $parent->ID ),
					);
				}
			}

			// Category
			if ( 'post' === $post->post_type ) {
				$categories = get_the_category( $post->ID );
				if ( ! empty( $categories ) ) {
					$category = $categories[0];
					$breadcrumbs[] = array(
						'name' => $category->name,
						'url'  => get_category_link( $category->term_id ),
					);
				}
			}

			// Current page/post (no link)
			$breadcrumbs[] = array(
				'name' => get_the_title( $post->ID ),
				'url'  => get_permalink( $post->ID ),
			);

			return $breadcrumbs;
		}

		// Archive pages
		if ( is_category() ) {
			$cat = get_queried_object();
			$breadcrumbs[] = array(
				'name' => $cat->name,
				'url'  => get_category_link( $cat->term_id ),
			);
			return $breadcrumbs;
		}

		if ( is_tag() ) {
			$tag = get_queried_object();
			$breadcrumbs[] = array(
				'name' => $tag->name,
				'url'  => get_tag_link( $tag->term_id ),
			);
			return $breadcrumbs;
		}

		if ( is_tax() ) {
			$term = get_queried_object();
			$breadcrumbs[] = array(
				'name' => $term->name,
				'url'  => get_term_link( $term ),
			);
			return $breadcrumbs;
		}

		if ( is_author() ) {
			$author = get_queried_object();
			$breadcrumbs[] = array(
				'name' => $author->display_name,
				'url'  => get_author_posts_url( $author->ID ),
			);
			return $breadcrumbs;
		}

		if ( is_date() ) {
			$breadcrumbs[] = array(
				'name' => __( 'Archives', 'bw-schema' ),
				'url'  => get_post_type_archive_link( 'post' ),
			);
			return $breadcrumbs;
		}

		// Custom post type archive
		if ( is_post_type_archive() ) {
			$post_type = get_queried_object();
			$breadcrumbs[] = array(
				'name' => $post_type->labels->name,
				'url'  => get_post_type_archive_link( $post_type->name ),
			);
			return $breadcrumbs;
		}

		// Search results
		if ( is_search() ) {
			$breadcrumbs[] = array(
				'name' => sprintf( __( 'Search Results for "%s"', 'bw-schema' ), get_search_query() ),
				'url'  => current_url(),
			);
			return $breadcrumbs;
		}

		return $breadcrumbs;
	}

	/**
	 * Get current URL
	 *
	 * @return string Current page URL
	 */
	protected function get_current_url() {
		global $wp;
		return home_url( $wp->request );
	}

	/**
	 * Validate breadcrumb schema
	 *
	 * Breadcrumb requires at least 2 items.
	 *
	 * @return array Validation errors
	 */
	public function validate() {
		$errors = parent::validate();

		$items = $this->schema['itemListElement'] ?? array();

		if ( count( $items ) < 2 ) {
			$errors[] = 'Breadcrumb schema requires at least 2 items';
		}

		return $errors;
	}
}
