<?php
/**
 * Diagnostic script to check Shop page configuration
 * Upload this to your WordPress root and access via browser
 */

// Load WordPress
require_once(__DIR__ . '/../../../wp-load.php');

if (!current_user_can('manage_options')) {
    die('Access denied');
}

echo '<h1>WooNooW Shop Page Diagnostic</h1>';

// 1. Check WooCommerce Shop Page ID
$shop_page_id = get_option('woocommerce_shop_page_id');
echo '<h2>1. WooCommerce Shop Page Setting</h2>';
echo '<p>Shop Page ID: ' . ($shop_page_id ? $shop_page_id : 'NOT SET') . '</p>';

if ($shop_page_id) {
    $shop_page = get_post($shop_page_id);
    if ($shop_page) {
        echo '<p>Shop Page Title: ' . esc_html($shop_page->post_title) . '</p>';
        echo '<p>Shop Page Status: ' . esc_html($shop_page->post_status) . '</p>';
        echo '<p>Shop Page URL: ' . get_permalink($shop_page_id) . '</p>';
        echo '<h3>Shop Page Content:</h3>';
        echo '<pre>' . esc_html($shop_page->post_content) . '</pre>';
        
        // Check for shortcode
        if (has_shortcode($shop_page->post_content, 'woonoow_shop')) {
            echo '<p style="color: green;">✓ Has [woonoow_shop] shortcode</p>';
        } else {
            echo '<p style="color: red;">✗ Missing [woonoow_shop] shortcode</p>';
        }
    } else {
        echo '<p style="color: red;">ERROR: Shop page not found!</p>';
    }
}

// 2. Find all pages with woonoow shortcodes
echo '<h2>2. Pages with WooNooW Shortcodes</h2>';
$pages_with_shortcodes = get_posts([
    'post_type' => 'page',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    's' => 'woonoow_',
]);

if (empty($pages_with_shortcodes)) {
    echo '<p style="color: orange;">No pages found with woonoow_ shortcodes</p>';
} else {
    echo '<ul>';
    foreach ($pages_with_shortcodes as $page) {
        echo '<li>';
        echo '<strong>' . esc_html($page->post_title) . '</strong> (ID: ' . $page->ID . ')<br>';
        echo 'URL: ' . get_permalink($page->ID) . '<br>';
        echo 'Content: <pre>' . esc_html(substr($page->post_content, 0, 200)) . '</pre>';
        echo '</li>';
    }
    echo '</ul>';
}

// 3. Check Customer SPA Settings
echo '<h2>3. Customer SPA Settings</h2>';
$spa_settings = get_option('woonoow_customer_spa_settings', []);
echo '<pre>' . print_r($spa_settings, true) . '</pre>';

// 4. Check if pages were created by installer
echo '<h2>4. WooNooW Page Options</h2>';
$woonoow_pages = [
    'shop' => get_option('woonoow_shop_page_id'),
    'cart' => get_option('woonoow_cart_page_id'),
    'checkout' => get_option('woonoow_checkout_page_id'),
    'account' => get_option('woonoow_account_page_id'),
];

foreach ($woonoow_pages as $key => $page_id) {
    echo '<p>' . ucfirst($key) . ' Page ID: ' . ($page_id ? $page_id : 'NOT SET');
    if ($page_id) {
        $page = get_post($page_id);
        if ($page) {
            echo ' - ' . esc_html($page->post_title) . ' (' . $page->post_status . ')';
        } else {
            echo ' - <span style="color: red;">PAGE NOT FOUND</span>';
        }
    }
    echo '</p>';
}

echo '<hr>';
echo '<h2>Recommended Actions:</h2>';
echo '<ol>';
echo '<li>If Shop page doesn\'t have [woonoow_shop] shortcode, add it to the page content</li>';
echo '<li>If Shop page ID doesn\'t match WooCommerce setting, update WooCommerce > Settings > Products > Shop Page</li>';
echo '<li>If SPA mode is "disabled", it will only load on pages with shortcodes</li>';
echo '<li>If SPA mode is "full", it will load on all WooCommerce pages</li>';
echo '</ol>';
