<?php
/**
 * Context Manager - The Brain
 * 
 * Determines when and where headers and footers should be displayed
 * based on global settings and page-specific rules.
 * 
 * @package Author_Website_Templates
 * @since 1.0.9
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Determine if header should be rendered on current page
 * 
 * Logic Sequence:
 * 1. Check global display setting
 * 1. Check per-page override meta field
 * 2. Check Force Global setting
 * 3. Check if current page is a plugin template
 * 4. Fallback (do not interfere with theme pages)
 * 
 * @return bool True if header should be rendered, false otherwise
 */
if ( ! function_exists( 'rswpthemes_awt_should_render_header' ) ) {
    function rswpthemes_awt_should_render_header() {
        // Priority 1: Check per-page override meta field (highest priority)
        if ( is_singular() || is_page() ) {
            $display = get_post_meta( get_the_ID(), '_awt_header_display', true );
            
            // Force disable (user can hide header even on Canvas)
            if ( $display === 'disable' ) {
                return apply_filters( 'rswpthemes_awt_should_render_header', false );
            }
            
            // Force enable
            if ( $display === 'enable' ) {
                return apply_filters( 'rswpthemes_awt_should_render_header', true );
            }
            
            // If 'default' or empty, continue to Priority 2
        }
        
        // Priority 2: Check Force Global setting
        $settings = get_option( 'awt_global_settings', [] );
        if ( isset( $settings['awt_header_force_global'] ) && $settings['awt_header_force_global'] === '1' ) {
            return apply_filters( 'rswpthemes_awt_should_render_header', true );
        }
        
        // Priority 3: Check if this is a plugin template (show header by default)
        // Single Book Post Type
        if ( is_singular( 'book' ) ) {
            return apply_filters( 'rswpthemes_awt_should_render_header', true );
        }
        
        // Book Archives and Taxonomies
        if ( is_post_type_archive( 'book' ) || 
             is_tax( 'book-category' ) || 
             is_tax( 'book-author' ) || 
             is_tax( 'book-series' ) ) {
            return apply_filters( 'rswpthemes_awt_should_render_header', true );
        }
        
        // AWT Canvas Page Template
        if ( is_page_template( 'awt-canvas' ) ) {
            return apply_filters( 'rswpthemes_awt_should_render_header', true );
        }
        
        // Priority 4: Fallback - Do not interfere with theme pages
        return apply_filters( 'rswpthemes_awt_should_render_header', false );
    }
}

/**
 * Determine if footer should be rendered on current page
 * 
 * Logic Sequence (same as header):
 * 1. Check per-page override meta field
 * 2. Check Force Global setting
 * 3. Check if current page is a plugin template
 * 4. Fallback (do not interfere with theme pages)
 * 
 * @return bool True if footer should be rendered, false otherwise
 */
if ( ! function_exists( 'rswpthemes_awt_should_render_footer' ) ) {
    function rswpthemes_awt_should_render_footer() {
        // Priority 1: Check per-page override meta field (highest priority)
        if ( is_singular() || is_page() ) {
            $display = get_post_meta( get_the_ID(), '_awt_footer_display', true );
            
            // Force disable (user can hide footer even on Canvas)
            if ( $display === 'disable' ) {
                return apply_filters( 'rswpthemes_awt_should_render_footer', false );
            }
            
            // Force enable
            if ( $display === 'enable' ) {
                return apply_filters( 'rswpthemes_awt_should_render_footer', true );
            }
            
            // If 'default' or empty, continue to Priority 2
        }
        
        // Priority 2: Check Force Global setting
        $settings = get_option( 'awt_global_settings', [] );
        if ( isset( $settings['awt_footer_force_global'] ) && $settings['awt_footer_force_global'] === '1' ) {
            return apply_filters( 'rswpthemes_awt_should_render_footer', true );
        }
        
        // Priority 3: Check if this is a plugin template (show footer by default)
        // Single Book Post Type
        if ( is_singular( 'book' ) ) {
            return apply_filters( 'rswpthemes_awt_should_render_footer', true );
        }
        
        // Book Archives and Taxonomies
        if ( is_post_type_archive( 'book' ) || 
             is_tax( 'book-category' ) || 
             is_tax( 'book-author' ) || 
             is_tax( 'book-series' ) ) {
            return apply_filters( 'rswpthemes_awt_should_render_footer', true );
        }
        
        // AWT Canvas Page Template
        if ( is_page_template( 'awt-canvas' ) ) {
            return apply_filters( 'rswpthemes_awt_should_render_footer', true );
        }
        
        // Priority 4: Fallback - Do not interfere with theme pages
        return apply_filters( 'rswpthemes_awt_should_render_footer', false );
    }
}

/**
 * Helper: Get active template slug
 * 
 * @return string Active template slug (default: 'child-author')
 */
if ( ! function_exists( 'rswpthemes_awt_get_active_template' ) ) {
    function rswpthemes_awt_get_active_template() {
        $settings = get_option( 'awt_global_settings', [] );
        $template = isset( $settings['awt_active_template'] ) ? $settings['awt_active_template'] : 'child-author';
        
        /**
         * Filter: Allow developers to override active template
         * 
         * @param string $template Active template slug
         */
        return apply_filters( 'rswpthemes_awt_active_template', $template );
    }
}
