<?php
/**
 * Context Builder - Collects page context for widget
 *
 * @package ReplyIt_Chat_Widget
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

class ReplyIt_Context {

    /**
     * Single instance
     */
    private static $instance = null;

    /**
     * Get instance
     */
    public static function instance() {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor
     */
    private function __construct() {
        // Context is built on-demand
    }

    /**
     * Get page context
     */
    public static function get_page_context() {
        $page_type = self::get_page_type();

        // Determine platform based on page type
        $platform = 'wordpress';
        if (class_exists('WooCommerce') && in_array($page_type, ['product', 'cart', 'checkout', 'account', 'shop'])) {
            $platform = 'woocommerce';
        }

        $context = [
            'platform' => $platform,
            'page_type' => $page_type,
            'page_url' => self::get_current_url(),
        ];

        // Add WooCommerce data if available
        if (class_exists('ReplyIt_WooCommerce')) {
            // Add cart data
            $cart = ReplyIt_WooCommerce::get_cart_data();
            if ($cart) {
                $context['cart'] = $cart;
            }

            // Add current product data
            $product = ReplyIt_WooCommerce::get_current_product();
            if ($product) {
                $context['current_product'] = $product;
            }

            // Add customer data
            $customer = ReplyIt_WooCommerce::get_customer_data();
            if ($customer) {
                $context['customer'] = $customer;
            }
        }

        // Add blog context if available
        if (class_exists('ReplyIt_Blog')) {
            // Add current post data (for single blog posts)
            $post = ReplyIt_Blog::get_current_post();
            if ($post) {
                $context['current_post'] = $post;
            }

            // Add category/tag archive data
            $archive = ReplyIt_Blog::get_archive_context();
            if ($archive) {
                $context['archive'] = $archive;
            }
        }

        return $context;
    }

    /**
     * Determine page type
     */
    private static function get_page_type() {
        // WooCommerce pages first (if WooCommerce is active)
        if (class_exists('WooCommerce')) {
            if (is_product()) {
                return 'product';
            }

            if (is_cart()) {
                return 'cart';
            }

            if (is_checkout()) {
                return 'checkout';
            }

            if (is_account_page()) {
                return 'account';
            }

            if (is_shop() || is_product_category() || is_product_tag()) {
                return 'shop';
            }
        }

        // Blog pages
        if (is_single() && get_post_type() === 'post') {
            return 'blog_post';
        }

        if (is_category()) {
            return 'blog_category';
        }

        if (is_tag()) {
            return 'blog_tag';
        }

        if (is_author()) {
            return 'blog_author';
        }

        if (is_home() || is_archive() && get_post_type() === 'post') {
            return 'blog_archive';
        }

        if (is_search()) {
            return 'search';
        }

        if (is_front_page()) {
            return 'home';
        }

        if (is_page()) {
            return 'page';
        }

        return 'page';
    }

    /**
     * Get current URL
     */
    private static function get_current_url() {
        global $wp;
        return home_url($wp->request);
    }
}
