<?php
/** Shared runtime for official DIY Church Website child themes. */

defined( 'ABSPATH' ) || exit;

/** Return this child theme's immutable configuration. */
function diy_church_child_config() {
	return isset( $GLOBALS['diy_church_child_config'] ) && is_array( $GLOBALS['diy_church_child_config'] ) ? $GLOBALS['diy_church_child_config'] : array();
}

/** Enqueue the child presentation layer after Cheetah Wireframe. */
function diy_church_child_assets() {
	$config = diy_church_child_config();
	wp_enqueue_style( 'diy-church-child', get_stylesheet_directory_uri() . '/assets/css/theme.css', array( 'cheetah-main' ), isset( $config['version'] ) ? $config['version'] : '1.0.0' );
}
add_action( 'wp_enqueue_scripts', 'diy_church_child_assets', 20 );

/** Expose the profile and optional demo manifest to Cheetah 2.4+. */
function diy_church_child_profile( $profile ) {
	$config = diy_church_child_config();
	if ( empty( $config ) ) {
		return $profile;
	}
	return array(
		'slug'        => $config['slug'],
		'name'        => $config['name'],
		'description' => $config['description'],
		'version'     => $config['version'],
		'demo'        => function_exists( 'cheetah_build_church_demo' ) ? cheetah_build_church_demo( $config['demo'] ) : array(),
	);
}
add_filter( 'cheetah_child_theme_profile', 'diy_church_child_profile' );

/** Make the child palette the default even when demo content is skipped. */
function diy_church_child_defaults( $defaults ) {
	$config = diy_church_child_config();
	$demo   = isset( $config['demo'] ) ? $config['demo'] : array();
	$map    = array(
		'color_primary' => 'primary', 'color_secondary' => 'secondary', 'color_accent' => 'accent',
		'color_body_bg' => 'body_bg', 'color_surface' => 'surface', 'color_text' => 'text',
		'color_headings' => 'headings', 'color_border' => 'border', 'color_link' => 'primary',
		'btn_bg' => 'primary', 'btn_bg_hover' => 'secondary', 'hero_overlay_color' => 'hero_overlay',
		'hero_style' => 'hero_style', 'heading_font' => 'heading_font', 'body_font' => 'body_font',
	);
	foreach ( $map as $option => $source ) {
		if ( isset( $demo[ $source ] ) ) {
			$defaults[ $option ] = $demo[ $source ];
		}
	}
	$defaults['border_radius'] = 12;
	$defaults['button_radius'] = 999;
	return $defaults;
}
add_filter( 'cheetah_defaults', 'diy_church_child_defaults', 20 );

/** Coordinate DIY Sermons with the active child palette. */
function diy_church_child_sermon_tokens( $tokens ) {
	$config = diy_church_child_config();
	$demo   = $config['demo'];
	$tokens['accent'] = $demo['primary'];
	$tokens['surface'] = $demo['surface'];
	$tokens['text'] = $demo['text'];
	$tokens['border'] = $demo['border'];
	$tokens['radius'] = '0.9rem';
	return $tokens;
}
add_filter( 'diy_sermons_design_tokens', 'diy_church_child_sermon_tokens' );

/** Coordinate DIY Church Events with the active child palette. */
function diy_church_child_event_tokens( $tokens ) {
	$config = diy_church_child_config();
	$demo   = $config['demo'];
	$tokens['primary'] = $demo['primary'];
	$tokens['surface'] = $demo['surface'];
	$tokens['text'] = $demo['text'];
	$tokens['border'] = $demo['border'];
	$tokens['radius'] = '0.9rem';
	return $tokens;
}
add_filter( 'diy_church_events_design_tokens', 'diy_church_child_event_tokens' );

/** Stable body class for patterns and extension styling. */
function diy_church_child_body_class( $classes ) {
	$config    = diy_church_child_config();
	$classes[] = 'diy-child-theme';
	$classes[] = 'diy-child-' . sanitize_html_class( $config['slug'] );
	return $classes;
}
add_filter( 'body_class', 'diy_church_child_body_class' );

/** Document the supported first-party integration contract. */
function diy_church_child_setup_supports() {
	add_theme_support( 'diy-sermons', array( 'campuses' => true, 'speakers' => true, 'design-tokens' => true ) );
	add_theme_support( 'diy-church-events', array( 'campuses' => true, 'speakers' => true, 'design-tokens' => true ) );
}
add_action( 'after_setup_theme', 'diy_church_child_setup_supports', 20 );

/** Explain the parent-version requirement without breaking the frontend. */
function diy_church_child_parent_notice() {
	if ( current_user_can( 'update_themes' ) && ( ! defined( 'CHEETAH_VERSION' ) || version_compare( CHEETAH_VERSION, '2.4.0', '<' ) ) ) {
		echo '<div class="notice notice-warning"><p>' . esc_html__( 'This child theme needs Cheetah Wireframe 2.4 or newer for its optional demo setup and rollback tools. The public design remains usable, but please update the parent theme before importing demo content.', 'cheetah-wireframe' ) . '</p></div>';
	}
}
add_action( 'admin_notices', 'diy_church_child_parent_notice' );
