<?php
/**
 * BlogiBot Toastr Helper
 * PHP helper class for working with the Toastr component
 */

if ( ! defined( 'ABSPATH' ) ) exit;

class BlogiBotToastr {
    
    /**
     * Enqueue Toastr assets
     */
    public static function enqueue_assets() {
        wp_enqueue_script(
            'blogibot-toastr',
            plugins_url('../../../assets/js/aiblog/toastr/toastr.js', __FILE__),
            array('jquery'),
            '1.0.0',
            true
        );
    }
    
    /**
     * Add toast notification via PHP (will be shown on next page load)
     * 
     * @param string $message The toast message
     * @param string $type Toast type: success, error, warning, info
     * @param array $options Additional toast options
     */
    public static function add_toast($message, $type = 'info', $options = array()) {
        $toasts = get_transient('blogibot_toasts_' . get_current_user_id()) ?: array();
        
        $toasts[] = array(
            'message' => $message,
            'type' => $type,
            'options' => $options,
            'timestamp' => time()
        );
        
        // Limit to 10 toasts and expire after 5 minutes
        if (count($toasts) > 10) {
            $toasts = array_slice($toasts, -10);
        }
        
        set_transient('blogibot_toasts_' . get_current_user_id(), $toasts, 300);
    }
    
    /**
     * Display pending toasts and clear them
     */
    public static function display_pending_toasts() {
        $user_id = get_current_user_id();
        $toasts = get_transient('blogibot_toasts_' . $user_id);
        
        if (empty($toasts)) {
            return;
        }
        
        echo '<script type="text/javascript">';
        echo 'document.addEventListener("DOMContentLoaded", function() {';
        echo 'if (window.BlogiToastr) {';
        
        foreach ($toasts as $toast) {
            $message = json_encode($toast['message']);
            $type = json_encode($toast['type']);
            $options = json_encode($toast['options']);
            echo "window.BlogiToastr.show($message, $type, $options);";
        }
        
        echo '}';
        echo '});';
        echo '</script>';
        
        // Clear the toasts
        delete_transient('blogibot_toasts_' . $user_id);
    }
    
    /**
     * Add success toast
     */
    public static function success($message, $options = array()) {
        self::add_toast($message, 'success', $options);
    }
    
    /**
     * Add error toast
     */
    public static function error($message, $options = array()) {
        self::add_toast($message, 'error', $options);
    }
    
    /**
     * Add warning toast
     */
    public static function warning($message, $options = array()) {
        self::add_toast($message, 'warning', $options);
    }
    
    /**
     * Add info toast
     */
    public static function info($message, $options = array()) {
        self::add_toast($message, 'info', $options);
    }
    
    /**
     * Add toast notification script for immediate display
     * Use this when you want to show a toast immediately via JavaScript
     */
    public static function add_immediate_toast($message, $type = 'info', $options = array()) {
        $message_js = json_encode($message);
        $type_js = json_encode($type);
        $options_js = json_encode($options);
        
        echo "<script>
        if (window.BlogiToastr) {
            window.BlogiToastr.show($message_js, $type_js, $options_js);
        } else {
            document.addEventListener('DOMContentLoaded', function() {
                if (window.BlogiToastr) {
                    window.BlogiToastr.show($message_js, $type_js, $options_js);
                }
            });
        }
        </script>";
    }
    
    /**
     * Initialize Toastr hooks
     */
    public static function init() {
        // Display pending toasts on admin pages
        add_action('admin_footer', array(__CLASS__, 'display_pending_toasts'));
        
        // Enqueue assets when needed
        add_action('wp_admin_enqueue_scripts', function() {
            $screen = get_current_screen();
            if ($screen && strpos($screen->id, 'blogibot') !== false) {
                self::enqueue_assets();
            }
        });
    }
}
