<!DOCTYPE html>
<html <?php language_attributes(); ?>>

<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php wp_title('|', true, 'right'); ?><?php bloginfo('name'); ?></title>
    <?php wp_head(); ?>
</head>

<body <?php body_class('woonoow-spa-page'); ?>>
    <?php
    // Determine initial route based on SPA mode
    $appearance_settings = get_option('woonoow_appearance_settings', []);
    $spa_mode = isset($appearance_settings['general']['spa_mode']) ? $appearance_settings['general']['spa_mode'] : 'full';

    // Get actual request path for accurate routing
    $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
    $current_path = parse_url($request_uri, PHP_URL_PATH);

    // Set initial page based on mode
    if ($spa_mode === 'checkout_only') {
        // Checkout Only mode starts at cart
        $page_type = 'cart';
        $data_attrs = 'data-page="cart" data-initial-route="/cart"';
    } else {
        // Evaluate WordPress page type to pass to React App
        if (is_product()) {
            global $post;
            $data_attrs = 'data-page="product" data-product-id="' . esc_attr($post->ID) . '"';
        } elseif (is_cart()) {
            $data_attrs = 'data-page="cart"';
        } elseif (is_checkout()) {
            $data_attrs = 'data-page="checkout"';
        } elseif (is_account_page()) {
            $data_attrs = 'data-page="account"';
        } elseif (is_singular('page')) {
            $data_attrs = 'data-page="page"';
        } elseif (is_singular() && !is_singular('page')) {
            global $post;
            $post_type = get_post_type();
            $data_attrs = 'data-page="cpt" data-cpt-type="' . esc_attr($post_type) . '" data-cpt-slug="' . esc_attr($post->post_name) . '"';
        } else {
            $data_attrs = 'data-page="shop"';
        }

        // If this is the front page, route to /
        if (is_front_page()) {
            $data_attrs .= ' data-initial-route="/"';
        } else {
            $data_attrs .= ' data-initial-route="' . esc_attr($current_path) . '"';
        }
    }
    ?>

    <div id="woonoow-customer-app" <?php echo $data_attrs; ?>>
        <div class="woonoow-loading" style="display: flex; align-items: center; justify-content: center; min-height: 100vh; font-family: system-ui, -apple-system, sans-serif;">
            <p><?php esc_html_e('Loading...', 'woonoow'); ?></p>
        </div>
    </div>

    <?php wp_footer(); ?>
</body>

</html>