<?php
/**
 * File Path: includes/compat/compat-divi.php
 * Divi Theme (Elegant Themes) Compatibility -- loaded when get_template() === 'divi'.
 * Findings reference: PATCHES/COMPAT-WORKLOG.md -- DV-001 through DV-006.
 * @package My_Book_Showroom
 * @since   1.0.0
 */

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

/**
 * DV-001 + DV-002 -- Divi module inner scope fix.
 * On Divi-built pages .mbs-wrapper lives inside .et_pb_module_inner, not .entry-content,
 * so all mbs-compat.css rules are inactive. This inline block mirrors those rules under
 * .et_pb_module_inner with !important on paragraph margins to beat Divi's ID-prefixed
 * selectors (specificity 1,2,1).
 */
add_action( 'wp_enqueue_scripts', static function () {

	if ( ! wp_style_is( 'mbs-front', 'enqueued' ) ) {
		return;
	}

	$css = '
		/* DV-001: mirror mbs-compat.css cascade protections for .et_pb_module_inner context */
		/* DV-002: !important on margins only — required to beat ID-prefixed Divi selectors (1,2,1) */

		/* Heading cascade */
		.et_pb_module_inner .mbs-wrapper h1,
		.et_pb_module_inner .mbs-wrapper h2,
		.et_pb_module_inner .mbs-wrapper h3,
		.et_pb_module_inner .mbs-wrapper h4,
		.et_pb_module_inner .mbs-wrapper h5,
		.et_pb_module_inner .mbs-wrapper h6 {
			margin: 0 0 0.5em;
			line-height: 1.3;
		}

		/* Paragraph cascade — !important required to beat Divi\'s #post-NNN selector chain */
		.et_pb_module_inner .mbs-wrapper p {
			margin: 0 0 0.75em !important;
			line-height: 1.6;
		}
		.et_pb_module_inner .mbs-wrapper p:last-child {
			margin-bottom: 0 !important;
		}

		/* List cascade */
		.et_pb_module_inner .mbs-wrapper ul,
		.et_pb_module_inner .mbs-wrapper ol {
			padding: 0 0 0 1.25em;
			margin: 0 0 0.75em;
		}
		.et_pb_module_inner .mbs-wrapper li {
			margin-bottom: 0.25em;
		}
		.et_pb_module_inner .mbs-wrapper .mbs-per-format-meta {
			padding: 0;
			margin: 0;
			list-style: none;
		}

		/* Link color isolation */
		.et_pb_module_inner .mbs-wrapper a {
			color: var(--mbs-link, #4AABB8);
		}
		.et_pb_module_inner .mbs-wrapper a:hover,
		.et_pb_module_inner .mbs-wrapper a:focus {
			color: var(--mbs-accent, #4AABB8);
		}

		/* Retailer buy button color restoration */
		.et_pb_module_inner .mbs-wrapper a.mbs-retailer-btn {
			color: var(--mbs-button-text, #FFFFFF);
			background-color: var(--mbs-button-bg, #2C3E6B);
			text-decoration: none;
		}

		/* Format tabs */
		.et_pb_module_inner .mbs-wrapper a.mbs-format-tab {
			text-decoration: none;
		}

		/* Image containment */
		.et_pb_module_inner .mbs-wrapper img {
			border: 0 none;
			box-shadow: none;
			float: none;
			margin: 0;
			vertical-align: middle;
		}
	';

	wp_add_inline_style( 'mbs-front', $css );

}, 99 );


/**
 * DV-005 -- Template override defense for single-mbs_book.php.
 *
 * Divi's Visual Builder can replace single templates through its own
 * template-resolver. When a user activates Divi's builder on an mbs_book
 * post, Divi may serve its generic single template instead of MBS's own,
 * causing all metabox-driven sections (buy links, ARC, formats, etc.) to
 * disappear. This filter reasserts the MBS single-book template for any
 * mbs_book singular view, regardless of Divi's builder state.
 *
 * Priority 99 runs after Divi's own template_include hooks.
 * file_exists() guard is a safety net only — template should always exist.
 */
add_filter( 'template_include', static function ( string $template ) : string {

	if ( ! is_singular( 'mbs_book' ) ) {
		return $template;
	}

	$mbs_template = MBS_TEMPLATES_DIR . 'single-mbs_book.php';
	return file_exists( $mbs_template ) ? $mbs_template : $template;

}, 99 );
/**
 * DV-004 note -- wpautop removal on Divi builder pages. [AI]
 * If MBS text sections lose paragraph wrapping on Divi-built pages, ensure template
 * partials call wp_kses_post(wpautop($content)) directly rather than relying on the_content.
 * Full analysis in PATCHES/COMPAT-WORKLOG.md -- DV-004.
 */
