<?php
/**
 * File Path: includes/class-mbs-compat-loader.php
 *
 * Theme Compatibility Loader
 *
 * Detects active theme and loads per-theme compat files.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

class MBS_Compat_Loader {

	public function __construct() {
		add_action( 'init', array( $this, 'detect_theme' ), 1 );
	}

	public function detect_theme(): void {
		$theme = get_template(); // parent theme slug

		$compat_files = array(
			'divi'          => 'compat-divi.php',
			'Avada'         => 'compat-avada.php',
			'astra'         => 'compat-astra.php',
			'oceanwp'       => 'compat-oceanwp.php',
			'flatsome'      => 'compat-flatsome.php',
			'betheme'       => 'compat-betheme.php',
			'newspaper'     => 'compat-newspaper.php',
			'graphene-plus' => 'compat-graphene-plus.php',
		);

		if ( isset( $compat_files[ $theme ] ) ) {
			$file = MBS_INCLUDES_DIR . 'compat/' . $compat_files[ $theme ];
			if ( file_exists( $file ) ) {
				require_once $file;
			}
		}

		// Compatibility Mode override from settings
		if ( mbs_get_setting( 'advanced', 'compatibility_mode', false ) ) {
			// file_exists() guards + !is_admin() check already present and correct
			if ( ! is_admin() ) {
				$css_file = MBS_ASSETS_DIR . 'css/mbs-compat.css';
				$js_file  = MBS_ASSETS_DIR . 'js/mbs-compat.js';

				if ( file_exists( $css_file ) ) {
					wp_enqueue_style( 'mbs-compat', MBS_ASSETS_URL . 'css/mbs-compat.css', array(), MBS_VERSION );
				}

				if ( file_exists( $js_file ) ) {
					wp_enqueue_script( 'mbs-compat', MBS_ASSETS_URL . 'js/mbs-compat.js', array( 'jquery' ), MBS_VERSION, true );
				}
			}
		}
	}
}
