<?php
/**
 * Edify <-> FluentCart integration.
 *
 * FluentCart's Cart / Checkout / Shop pages are normal WordPress Pages that
 * hold FluentCart shortcodes ([fluent_cart_cart], [fluent_cart_checkout], etc.)
 * and are rendered through the theme's own page.php. FluentCart is not
 * WooCommerce, so it never touches anything under /woocommerce/edify-woocommerce.php
 * and none of that file's hooks, wrappers, or CSS apply to it.
 *
 * This file gives FluentCart's own page types a body class to target, and
 * loads a small stylesheet that reconciles Edify's global Bootstrap /
 * entry-content typography rules with FluentCart's own scoped fct-* markup.
 */

if ( ! defined( 'ABSPATH' ) ) {
	die( 'Forbidden.' );
}

/**
 * Add a body class for the current FluentCart page type (cart, checkout,
 * shop, single_product, product_taxonomy, customer_dashboard, etc.) so CSS
 * (and child themes) can target them individually.
 */
function edify_fluentcart_body_class( $classes ) {

	if ( ! class_exists( '\FluentCart\App\Services\TemplateService' ) ) {
		return $classes;
	}

	$page_type = \FluentCart\App\Services\TemplateService::getCurrentFcPageType();

	if ( $page_type ) {
		$classes[] = 'edify-fc-page';
		$classes[] = 'edify-fc-page-' . sanitize_html_class( str_replace( '_', '-', $page_type ) );
	}

	return $classes;
}
add_filter( 'body_class', 'edify_fluentcart_body_class' );

/**
 * Enqueue the FluentCart compatibility stylesheet only on FluentCart pages,
 * after the theme's own styles so it can cleanly override the handful of
 * global rules that clash (see assets/css/edify-fluentcart.css for what and
 * why, each rule is commented).
 */
function edify_fluentcart_enqueue_assets() {

	if ( ! class_exists( '\FluentCart\App\Services\TemplateService' ) ) {
		return;
	}

	$page_type = \FluentCart\App\Services\TemplateService::getCurrentFcPageType();

	if ( ! $page_type ) {
		return;
	}

	wp_enqueue_style(
		'edify-fluentcart',
		EDIFY_CSS . '/edify-fluentcart.css',
		array( 'edify-main-style', 'bootstrap' ),
		EDIFY_VERSION
	);
}
add_action( 'wp_enqueue_scripts', 'edify_fluentcart_enqueue_assets', 20 );
