<?php
namespace Lean_CSNS;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Centralized AJAX actions handler
 */
class Ajax_Actions {
    
    /**
     * Singleton instance
     * 
     * @var Ajax_Actions
     */
    private static $instance = null;
    
    /**
     * Get singleton instance
     * 
     * @return Ajax_Actions
     */
    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * Private constructor to prevent direct instantiation
     */
    private function __construct() {
        $this->init();
    }
    
    /**
     * Initialize AJAX actions
     */
    private function init() {
        // AJAX hooks for logged-in users
        add_action('wp_ajax_lean_csns_share_cart', [$this, 'share_cart']);
        add_action('wp_ajax_lean_csns_save_cart', [$this, 'save_cart']);
        add_action('wp_ajax_lean_csns_delete_saved_cart', [$this, 'delete_saved_cart']);
        
        // AJAX hooks for non-logged-in users (only for sharing)
        add_action('wp_ajax_nopriv_lean_csns_share_cart', [$this, 'share_cart']);
    }
    
    /**
     * AJAX handler for sharing cart
     */
    public function share_cart() {
        check_ajax_referer('lean_csns_nonce', 'nonce');
        
        $cart_handler = Cart_Handler::get_instance();
        $result = $cart_handler->share_cart();

        if ( $result['success'] ) {
            // Neutral extension point — pro injects pre-rendered popup HTML via this filter.
            $result['popup_html'] = apply_filters( 'lean_csns_share_popup_html', '', $result['url'], $result['token'] );
        }

        wp_send_json($result);
    }
    
    /**
     * AJAX handler for saving cart
     */
    public function save_cart() {
        check_ajax_referer('lean_csns_nonce', 'nonce');
        
        $name = sanitize_text_field(wp_unslash($_POST['name'] ?? ''));
        if (empty($name)) {
            wp_send_json(['success' => false, 'message' => __('Cart name is required', 'lean-cart-share-and-save')]);
        }
        
        $cart_handler = Cart_Handler::get_instance();
        $result = $cart_handler->save_cart($name);
        
        wp_send_json($result);
    }
    
    /**
     * AJAX handler for deleting saved cart
     */
    public function delete_saved_cart() {
        check_ajax_referer('lean_csns_nonce', 'nonce');
        
        if (!is_user_logged_in()) {
            wp_send_json(['success' => false, 'message' => __('Please log in to delete cart', 'lean-cart-share-and-save')]);
        }
        
        $cart_id = absint($_POST['cart_id'] ?? 0);
        if (!$cart_id) {
            wp_send_json(['success' => false, 'message' => __('Invalid cart ID', 'lean-cart-share-and-save')]);
        }
        
        $user_id = get_current_user_id();
        
        // Verify ownership
        if (!Database::get_instance()->verify_cart_ownership($cart_id, $user_id)) {
            wp_send_json(['success' => false, 'message' => __('You are not authorized to delete this cart', 'lean-cart-share-and-save')]);
        }
        
        $result = Database::get_instance()->delete_cart($cart_id);
        
        wp_send_json([
            'success' => $result ? true : false,
            'message' => $result ? __('Cart deleted successfully!', 'lean-cart-share-and-save') : __('Failed to delete cart', 'lean-cart-share-and-save')
        ]);
    }
}

// Initialize singleton instance
Ajax_Actions::get_instance();