<?php
/**
 * File Path: includes/class-mbs-book-display.php
 *
 * Book Display Handler
 *
 * Central class for rendering single book and archive pages with theme override support.
 *
 */

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

class MBS_Book_Display {

	/**
	 * Hook template_include to serve MBS templates with theme override support (H-19).
	 */
	public function __construct() {
		add_filter( 'template_include', array( $this, 'template_loader' ), 99 );
	}

	/**
	 * Load the correct template with theme override priority.
	 *
	 * Checks theme/my-book-showroom/ first, then plugin templates/.
	 */
	public function template_loader( string $template ): string {
		if ( is_singular( MBS_POST_TYPE ) ) {

			// S14-8: PRO landing page mode — checked before standard template resolution.
			// `single-mbs-book.php` is completely unaffected for non-landing books.
			if ( mbs_is_pro() && get_post_meta( get_the_ID(), 'mbs_landing_page_mode', true ) === '1' ) {
				$theme_landing = get_stylesheet_directory() . '/my-book-showroom/single-mbs-book-landing.php';
				if ( file_exists( $theme_landing ) ) { return $theme_landing; }
				$parent_landing = get_template_directory() . '/my-book-showroom/single-mbs-book-landing.php';
				if ( file_exists( $parent_landing ) ) { return $parent_landing; }
				return MBS_TEMPLATES_DIR . 'single-mbs-book-landing.php';
			}

			$theme_file = get_stylesheet_directory() . '/my-book-showroom/single-mbs-book.php';
			if ( file_exists( $theme_file ) ) {
				return $theme_file;
			}
			$parent_file = get_template_directory() . '/my-book-showroom/single-mbs-book.php';
			if ( file_exists( $parent_file ) ) {
				return $parent_file;
			}
			return MBS_TEMPLATES_DIR . 'single-mbs-book.php';
		}

		if ( is_post_type_archive( MBS_POST_TYPE ) ) {
			$theme_file = get_stylesheet_directory() . '/my-book-showroom/archive-mbs-book.php';
			if ( file_exists( $theme_file ) ) {
				return $theme_file;
			}
			$parent_file = get_template_directory() . '/my-book-showroom/archive-mbs-book.php';
			if ( file_exists( $parent_file ) ) {
				return $parent_file;
			}
			return MBS_TEMPLATES_DIR . 'archive-mbs-book.php';
		}

		// ARCH-05: Taxonomy templates now get the same theme-override treatment as
		// single and archive templates.
		$taxonomy_map = array(
			MBS_TAX_AUTHOR     => 'taxonomy-mbs-author.php',
			MBS_TAX_SERIES     => 'taxonomy-mbs-series.php',
			MBS_TAX_GENRE      => 'taxonomy-mbs-genre.php',
			MBS_TAX_TAG        => 'taxonomy-mbs-tag.php',
			MBS_TAX_COLLECTION => 'taxonomy-mbs-collection.php',
			MBS_TAX_UNIVERSE   => 'taxonomy-mbs-universe.php',
		);
		foreach ( $taxonomy_map as $taxonomy => $filename ) {
			if ( is_tax( $taxonomy ) ) {
				$theme_file = get_stylesheet_directory() . '/my-book-showroom/' . $filename;
				if ( file_exists( $theme_file ) ) {
					return $theme_file;
				}
				$parent_file = get_template_directory() . '/my-book-showroom/' . $filename;
				if ( file_exists( $parent_file ) ) {
					return $parent_file;
				}
				return MBS_TEMPLATES_DIR . $filename;
			}
		}

		return $template;
	}
}
