<?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();
}

require_once get_stylesheet_directory() . '/inc/demo-content.php';

/** Enqueue the child presentation layer after DIY Church Wireframe. */
function diy_church_child_assets() {
	$config = diy_church_child_config();
	wp_enqueue_style( 'sanctuary-fonts', 'https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Playfair+Display:wght@400;500;600&display=swap', array(), null );
	wp_enqueue_style( 'diy-church-child', get_stylesheet_directory_uri() . '/assets/css/theme.css', array( 'diy-church-main' ), isset( $config['version'] ) ? $config['version'] : '1.0.0' );
	wp_enqueue_style( 'sanctuary-showcase', get_stylesheet_directory_uri() . '/assets/css/showcase.css', array( 'diy-church-child', 'sanctuary-fonts' ), 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 DIY Church 2.4+. */
function diy_church_child_profile( $profile ) {
	$config = diy_church_child_config();
	if ( empty( $config ) ) {
		return $profile;
	}
	$demo = function_exists( 'diy_church_build_church_demo' ) ? diy_church_build_church_demo( $config['demo'] ) : array();
	if ( $demo && function_exists( 'sanctuary_extend_demo_manifest' ) ) {
		$demo = sanctuary_extend_demo_manifest( $demo );
	}
	return array(
		'slug'        => $config['slug'],
		'name'        => $config['name'],
		'description' => $config['description'],
		'version'     => $config['version'],
		'demo'        => $demo,
	);
}
add_filter( 'diy_church_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( 'diy_church_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' );

/** The screenshot-matched front page supplies its own cinematic hero. */
function sanctuary_front_page_hero( $show ) {
	return is_front_page() ? false : $show;
}
add_filter( 'diy_church_show_hero', 'sanctuary_front_page_hero', 30 );

/** 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 );

/** Keep the About page graceful when DIY Sermons is intentionally not installed. */
function sanctuary_register_speaker_directory_fallback() {
	if ( ! shortcode_exists( 'diy_speakers' ) ) {
		add_shortcode( 'diy_speakers', '__return_empty_string' );
	}
}
add_action( 'init', 'sanctuary_register_speaker_directory_fallback', 99 );

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