<?php
namespace WooNooW\Frontend;

/**
 * Shortcodes Manager
 * Handles WooNooW customer-facing shortcodes
 */
class Shortcodes {
    
    /**
     * Initialize
     */
    public static function init() {
        add_shortcode('woonoow_shop', [__CLASS__, 'shop_shortcode']);
        add_shortcode('woonoow_cart', [__CLASS__, 'cart_shortcode']);
        add_shortcode('woonoow_checkout', [__CLASS__, 'checkout_shortcode']);
        add_shortcode('woonoow_account', [__CLASS__, 'account_shortcode']);
    }
    
    /**
     * Shop shortcode
     * Usage: [woonoow_shop]
     */
    public static function shop_shortcode($atts) {
        $atts = shortcode_atts([
            'category' => '',
            'per_page' => 12,
        ], $atts);
        
        ob_start();
        ?>
        <div id="woonoow-customer-app" data-page="shop" data-category="<?php echo esc_attr($atts['category']); ?>" data-per-page="<?php echo esc_attr($atts['per_page']); ?>">
            <!-- Customer SPA will mount here -->
            <div class="woonoow-loading">
                <p><?php esc_html_e('Loading shop...', 'woonoow'); ?></p>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
    
    /**
     * Cart shortcode
     * Usage: [woonoow_cart]
     */
    public static function cart_shortcode($atts) {
        ob_start();
        ?>
        <div id="woonoow-customer-app" data-page="cart">
            <!-- Customer SPA will mount here -->
            <div class="woonoow-loading">
                <p><?php esc_html_e('Loading cart...', 'woonoow'); ?></p>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
    
    /**
     * Checkout shortcode
     * Usage: [woonoow_checkout]
     */
    public static function checkout_shortcode($atts) {
        // Require user to be logged in for checkout
        if (!is_user_logged_in()) {
            return '<div class="woonoow-notice">' . 
                   '<p>' . esc_html__('Please log in to proceed to checkout.', 'woonoow') . '</p>' .
                   '<a href="' . esc_url(wp_login_url(get_permalink())) . '" class="button">' . 
                   esc_html__('Log In', 'woonoow') . '</a>' .
                   '</div>';
        }
        
        ob_start();
        ?>
        <div id="woonoow-customer-app" data-page="checkout">
            <!-- Customer SPA will mount here -->
            <div class="woonoow-loading">
                <p><?php esc_html_e('Loading checkout...', 'woonoow'); ?></p>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
    
    /**
     * Account shortcode
     * Usage: [woonoow_account]
     */
    public static function account_shortcode($atts) {
        // Require user to be logged in
        if (!is_user_logged_in()) {
            return '<div class="woonoow-notice">' . 
                   '<p>' . esc_html__('Please log in to view your account.', 'woonoow') . '</p>' .
                   '<a href="' . esc_url(wp_login_url(get_permalink())) . '" class="button">' . 
                   esc_html__('Log In', 'woonoow') . '</a>' .
                   '</div>';
        }
        
        ob_start();
        ?>
        <div id="woonoow-customer-app" data-page="account">
            <!-- Customer SPA will mount here -->
            <div class="woonoow-loading">
                <p><?php esc_html_e('Loading account...', 'woonoow'); ?></p>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
}
