<?php
/**
 * Elementor Integration
 *
 * All Elementor-related functions and integrations for the theme.
 *
 * @package Paws_And_Care
 * @since 0.1.2
 */

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

// ========================================
// ELEMENTOR THEME SUPPORT
// ========================================

/**
 * Add Elementor Theme Support
 * 
 * Registers theme support for Elementor page builder and
 * enables theme location integration for header, footer, etc.
 */
function paws_and_care_elementor_support()
{
    // Add basic Elementor theme support
    add_theme_support('elementor');

    // Register Elementor theme locations when Elementor is loaded
    if (did_action('elementor/loaded')) {
        add_action('elementor/theme/register_locations', 'paws_and_care_register_elementor_locations');
    }
}
add_action('after_setup_theme', 'paws_and_care_elementor_support');

// ========================================
// DISABLE ELEMENTOR'S FONT AWESOME 4
// ========================================

/**
 * Disable Elementor's Font Awesome 4 Support (version-specific)
 * 
 * Elementor loads Font Awesome 4 by default which conflicts with the theme's FA6.
 * This only dequeues FA if it's version 4.x, leaving the theme's FA6 untouched.
 */
add_action('wp_enqueue_scripts', function () {
    global $wp_styles;

    // Check if font-awesome is registered
    if (isset($wp_styles->registered['font-awesome'])) {
        $fa_version = $wp_styles->registered['font-awesome']->ver;

        // Only dequeue if it's FA4 (version less than 5.0)
        if (version_compare($fa_version, '5.0', '<')) {
            wp_dequeue_style('font-awesome');
            wp_deregister_style('font-awesome');

            // Re-register theme's FA6
            wp_enqueue_style('font-awesome', PAWS_AND_CARE_URI . '/assets/fontawesome-free-6.5.1-web/css/all.min.css', array(), '6.5.1');
        }
    }
}, 100);



/**
 * Register Elementor Theme Locations
 * 
 * Registers all core locations (header, footer, single, archive)
 * allowing Elementor Pro Theme Builder to override these areas.
 *
 * @param \ElementorPro\Modules\ThemeBuilder\Classes\Locations_Manager $elementor_theme_manager
 */
function paws_and_care_register_elementor_locations($elementor_theme_manager)
{
    $elementor_theme_manager->register_all_core_locations();
}

// ========================================
// ELEMENTOR HELPER FUNCTIONS
// ========================================

/**
 * Check if current page is built with Elementor
 * 
 * Helper function to detect if the current page uses Elementor.
 * Useful for conditionally loading/hiding theme elements.
 *
 * @return bool True if page is built with Elementor
 */
function paws_and_care_is_elementor_page()
{
    // Skip if Elementor not loaded
    if (!did_action('elementor/loaded')) {
        return false;
    }

    // Skip if no valid post ID (e.g., in customizer)
    $post_id = get_the_ID();
    if (!$post_id) {
        return false;
    }

    // Skip if Elementor Plugin not available
    if (!class_exists('\Elementor\Plugin') || !isset(\Elementor\Plugin::$instance->documents)) {
        return false;
    }

    // Get the document and check if built with Elementor
    $document = \Elementor\Plugin::$instance->documents->get($post_id);
    if (!$document) {
        return false;
    }

    return $document->is_built_with_elementor();
}

// ========================================
// ELEMENTOR LAYOUT ADJUSTMENTS
// ========================================

/**
 * Auto-adjust layout for Elementor pages
 * 
 * Automatically sets full-width layout (no sidebar) for pages
 * that are built with Elementor. Only affects Elementor pages.
 *
 * @param array $layout The current layout configuration
 * @return array Modified layout for Elementor pages
 */
function paws_and_care_elementor_auto_layout($layout)
{
    // Only adjust if we're on a singular page and Elementor is active
    if (!is_singular() || !function_exists('paws_and_care_is_elementor_page')) {
        return $layout;
    }

    // Check if current page is built with Elementor
    if (paws_and_care_is_elementor_page()) {
        $layout['position'] = 'none';
        $layout['show_sidebar'] = false;
        $layout['content_class'] = 'col-12';
        $layout['sidebar_class'] = '';
        $layout['content_order'] = '';
        $layout['sidebar_order'] = '';
    }

    return $layout;
}
add_filter('paws_and_care_sidebar_layout', 'paws_and_care_elementor_auto_layout');
