<?php
/**
 * Meta Boxes for Author Website Templates
 * 
 * Provides per-page/post override controls for header and footer display.
 * Allows users to force enable or disable header/footer regardless of global settings.
 * 
 * @package Author_Website_Templates
 * @since 1.0.9
 */

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

/**
 * Register Meta Box
 */
function rswpthemes_awt_register_meta_boxes() {
    add_meta_box(
        'awt_template_options',
        __( 'Author Template Options', 'author-website-templates' ),
        'rswpthemes_awt_render_meta_box',
        array( 'page', 'post', 'book' ),
        'side',
        'default'
    );
}
add_action( 'add_meta_boxes', 'rswpthemes_awt_register_meta_boxes' );

/**
 * Render Meta Box Content
 */
function rswpthemes_awt_render_meta_box( $post ) {
    // Add nonce for security
    wp_nonce_field( 'awt_meta_box_nonce_action', 'awt_meta_box_nonce' );
    
    // Get current values
    $header_display = get_post_meta( $post->ID, '_awt_header_display', true );
    $footer_display = get_post_meta( $post->ID, '_awt_footer_display', true );
    
    // Default to 'default' if empty
    if ( empty( $header_display ) ) {
        $header_display = 'default';
    }
    if ( empty( $footer_display ) ) {
        $footer_display = 'default';
    }
    
    // Display options
    $options = array(
        'default' => __( 'Default (Follow Global Settings)', 'author-website-templates' ),
        'enable'  => __( 'Enable (Force Show)', 'author-website-templates' ),
        'disable' => __( 'Disable (Force Hide)', 'author-website-templates' ),
    );
    ?>
    
    <style>
        .awt-meta-field {
            margin-bottom: 15px;
        }
        .awt-meta-field label {
            display: block;
            font-weight: 600;
            margin-bottom: 5px;
        }
        .awt-meta-field select {
            width: 100%;
        }
        .awt-meta-field .description {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
    </style>
    
    <div class="awt-meta-fields">
        <!-- Header Display Control -->
        <div class="awt-meta-field">
            <label for="awt_header_display">
                <?php esc_html_e( 'Header Display', 'author-website-templates' ); ?>
            </label>
            <select name="awt_header_display" id="awt_header_display">
                <?php foreach ( $options as $value => $label ) : ?>
                    <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $header_display, $value ); ?>>
                        <?php echo esc_html( $label ); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <p class="description">
                <?php esc_html_e( 'Override global header settings for this page.', 'author-website-templates' ); ?>
            </p>
        </div>
        
        <!-- Footer Display Control -->
        <div class="awt-meta-field">
            <label for="awt_footer_display">
                <?php esc_html_e( 'Footer Display', 'author-website-templates' ); ?>
            </label>
            <select name="awt_footer_display" id="awt_footer_display">
                <?php foreach ( $options as $value => $label ) : ?>
                    <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $footer_display, $value ); ?>>
                        <?php echo esc_html( $label ); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <p class="description">
                <?php esc_html_e( 'Override global footer settings for this page.', 'author-website-templates' ); ?>
            </p>
        </div>
    </div>
    
    <?php
}

/**
 * Save Meta Box Data
 */
function rswpthemes_awt_save_meta_box( $post_id ) {
    // Check nonce
    if ( ! isset( $_POST['awt_meta_box_nonce'] ) || 
         ! wp_verify_nonce( $_POST['awt_meta_box_nonce'], 'awt_meta_box_nonce_action' ) ) {
        return;
    }
    
    // Check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    
    // Check permissions
    $post_type = get_post_type( $post_id );
    if ( $post_type === 'page' ) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }
    } else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }
    
    // Sanitize and save header display
    if ( isset( $_POST['awt_header_display'] ) ) {
        $header_display = sanitize_text_field( $_POST['awt_header_display'] );
        
        // Validate value
        if ( in_array( $header_display, array( 'default', 'enable', 'disable' ), true ) ) {
            update_post_meta( $post_id, '_awt_header_display', $header_display );
        }
    }
    
    // Sanitize and save footer display
    if ( isset( $_POST['awt_footer_display'] ) ) {
        $footer_display = sanitize_text_field( $_POST['awt_footer_display'] );
        
        // Validate value
        if ( in_array( $footer_display, array( 'default', 'enable', 'disable' ), true ) ) {
            update_post_meta( $post_id, '_awt_footer_display', $footer_display );
        }
    }
}
add_action( 'save_post', 'rswpthemes_awt_save_meta_box' );
