<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Template Name: Front Page
 */

// Check if user has set homepage to show latest posts
if ('posts' == get_option('show_on_front')) {
    // If set to show latest posts, load index.php instead
    get_template_part( 'index' );
    return;
}

// Check if user has set a specific page as homepage that's not this template
if ('page' == get_option('show_on_front')) {
    $page_on_front = get_option('page_on_front');
    if ($page_on_front && is_front_page() && !is_page_template('front-page.php')) {
        // If a different page is set as homepage, load page.php
        get_template_part( 'page' );
        return;
    }
}

get_header(); ?>

<?php
/**
 * Dynamic Front Page Sections
 *
 * Load sections based on the Homepage Layout Manager.
 * This allows users to control sections via the drag-and-drop interface.
 */

// Check if Matcha Extra plugin is active via class_exists (avoid loading wp-admin files on frontend)
$plugin_active = class_exists( 'Matcha_Extra' ) || ( function_exists( 'matcha_extra_init' ) );

// Only show frontpage sections if plugin is active
if (!$plugin_active) {
    // Show a simple message or default content when plugin is not active
    ?>
    <div class="pawfect-plugin-required">
        <div class="container">
            <div class="pawfect-plugin-notice">
                <h2><?php esc_html_e('Unlock Your Website\'s Full Potential', 'pawfect'); ?></h2>
                <p><?php esc_html_e('Install the Matcha Extra plugin to access advanced frontpage sections and enhanced customization options.', 'pawfect'); ?></p>
                <a href="<?php echo esc_url( admin_url('customize.php') ); ?>" class="button button-primary">
                    <?php esc_html_e('Customize Your Site', 'pawfect'); ?>
                </a>
            </div>
        </div>
    </div>
    <style>
    .pawfect-plugin-required {
        padding: 60px 0;
        text-align: center;
        background: #f8f9fa;
    }
    .pawfect-plugin-notice h2 {
        color: #333;
        margin-bottom: 20px;
    }
    .pawfect-plugin-notice p {
        color: #666;
        font-size: 16px;
        margin-bottom: 30px;
    }
    </style>
    <?php
    get_footer();
    return;
}

// Get enabled sections in order from the Layout Manager
$enabled_sections = Helpers::get_enabled_frontpage_sections();
?>

<main id="primary-content" class="pawfect-front-page-content" tabindex="-1">

<?php

// Define sections with their template paths
$sections_map = [
    'hero_slider' => [
        'template' => 'template-parts/front-page/hero-slider',
    ],
    'press_mentions' => [
        'template' => 'template-parts/front-page/press-mentions',
    ],
    'feature_highlight' => [
        'template' => 'template-parts/front-page/feature-highlight',
    ],
    'why_choose' => [
        'template' => 'template-parts/front-page/why-choose',
    ],
    'portfolio' => [
        'template' => 'template-parts/front-page/section-category-highlights',
    ],
    'featured_collection' => [
        'template' => 'template-parts/front-page/featured-collection',
    ],
    'testimonial' => [
        'template' => 'template-parts/front-page/index-testimonial',
    ],
    'mission' => [
        'template' => 'template-parts/front-page/index-mission',
    ],
    'pet_shop' => [
        'template' => 'template-parts/front-page/index-shop-by-pet',
    ],
    'promo_timer' => [
        'template' => 'template-parts/front-page/index-promo-timer',
    ],
    'crafted' => [
        'template' => 'template-parts/front-page/index-crafted',
    ],
    'blog' => [
        'template' => 'template-parts/front-page/index-blog',
    ],
    'promo_banner' => [
        'template' => 'template-parts/front-page/index-promo-banner',
    ],
    'instagram' => [
        'template' => 'template-parts/front-page/index-instagram',
    ],

    'woocommerce' => [
        'template' => 'template-parts/front-page/section-woocommerce-featured',
    ],
];

// Loop through enabled sections and load their template parts
foreach ( $enabled_sections as $section_id ) {
    // Skip if section doesn't exist in our mapping
    if ( ! isset( $sections_map[ $section_id ] ) ) {
        continue;
    }

    $section_data = $sections_map[ $section_id ];
    $template_path = $section_data['template'];

    // Load the template part if it exists
    if ( locate_template( $template_path . '.php' ) ) {
        get_template_part( $template_path );
    }
}
?>

</main>

<?php get_footer(); ?>