<?php
/**
 * Displays footer widgets if assigned
 *
 * @package AI Automation
 * @subpackage ai_automation
 */
?>
<?php

// Determine the number of columns dynamically for the footer (you can replace this with your logic).
$ai_automation_no_of_footer_col = get_theme_mod('ai_automation_footer_columns', 4); // Change this value as needed.

// Calculate the Bootstrap class for large screens (col-lg-X) for footer.
$ai_automation_col_lg_footer_class = 'col-lg-' . (12 / $ai_automation_no_of_footer_col);

// Calculate the Bootstrap class for medium screens (col-md-X) for footer.
$ai_automation_col_md_footer_class = 'col-md-' . (12 / $ai_automation_no_of_footer_col);
?>
<div class="container">
    <aside class="widget-area row py-2 pt-3" role="complementary" aria-label="<?php esc_attr_e( 'Footer', 'ai-automation' ); ?>">
        <?php
        $ai_automation_default_widgets = array(
            1 => 'search',
            2 => 'archives',
            3 => 'meta',
            4 => 'categories'
        );

        for ($ai_automation_i = 1; $ai_automation_i <= $ai_automation_no_of_footer_col; $ai_automation_i++) :
            $ai_automation_lg_class = esc_attr($ai_automation_col_lg_footer_class);
            $ai_automation_md_class = esc_attr($ai_automation_col_md_footer_class);
            echo '<div class="col-12 ' . $ai_automation_lg_class . ' ' . $ai_automation_md_class . '">';

            if (is_active_sidebar('footer-' . $ai_automation_i)) {
                dynamic_sidebar('footer-' . $ai_automation_i);
            } else {
                // Display default widget content if not active.
                switch ($ai_automation_default_widgets[$ai_automation_i] ?? '') {
                    case 'search':
                        ?>
                        <aside class="widget" role="complementary" aria-label="<?php esc_attr_e('Search', 'ai-automation'); ?>">
                            <h3 class="widget-title"><?php esc_html_e('Search', 'ai-automation'); ?></h3>
                            <?php get_search_form(); ?>
                        </aside>
                        <?php
                        break;

                    case 'archives':
                        ?>
                        <aside class="widget" role="complementary" aria-label="<?php esc_attr_e('Archives', 'ai-automation'); ?>">
                            <h3 class="widget-title"><?php esc_html_e('Archives', 'ai-automation'); ?></h3>
                            <ul><?php wp_get_archives(['type' => 'monthly']); ?></ul>
                        </aside>
                        <?php
                        break;

                    case 'meta':
                        ?>
                        <aside class="widget" role="complementary" aria-label="<?php esc_attr_e('Meta', 'ai-automation'); ?>">
                            <h3 class="widget-title"><?php esc_html_e('Meta', 'ai-automation'); ?></h3>
                            <ul>
                                <?php wp_register(); ?>
                                <li><?php wp_loginout(); ?></li>
                                <?php wp_meta(); ?>
                            </ul>
                        </aside>
                        <?php
                        break;

                    case 'categories':
                        ?>
                        <aside class="widget" role="complementary" aria-label="<?php esc_attr_e('Categories', 'ai-automation'); ?>">
                            <h3 class="widget-title"><?php esc_html_e('Categories', 'ai-automation'); ?></h3>
                            <ul><?php wp_list_categories(['title_li' => '']); ?></ul>
                        </aside>
                        <?php
                        break;
                }
            }

            echo '</div>';
        endfor;
        ?>
    </aside>
</div>