/** * AiWorkflowModal — Natural Language Text-to-Workflow Generator. * * Allows users to generate a complete visual automation workflow by * describing their goal in natural language (English or Bengali), or * by clicking one of the pre-made high-converting recipes. */ import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Modal, Button, TextareaControl, SelectControl } from '../../common/WpComponents'; import { apiPost } from '../../../utils/api'; import { toast } from '../../common/Toast'; import LoadingBtn from '../../common/LoadingBtn'; const POPULAR_USE_CASES = [ { id: 'cart_recovery', icon: '🛒', title: __( 'Abandoned Cart Recovery', 'ai-marketing-expert' ), badge: __( 'E-Commerce', 'ai-marketing-expert' ), prompt: __( 'When a customer abandons their WooCommerce cart, send a personalized AI recovery email with their product details and restore link {event.recovery_url}, then send an email notification to the store manager.', 'ai-marketing-expert' ), }, { id: 'smart_cart_recovery', icon: '🎯', title: __( 'Smart Cart Recovery (Single Item / Duplicate Qty)', 'ai-marketing-expert' ), badge: __( 'High Conversion', 'ai-marketing-expert' ), prompt: __( 'When a customer abandons their WooCommerce cart, branch on whether they added duplicate quantities. If cart_type equals duplicate_qty, send a 1-click single-quantity checkout email with {event.single_qty_url}. Otherwise, send a recovery email offering 1-click single-item links {event.single_item_links} or full restore {event.recovery_url}.', 'ai-marketing-expert' ), }, { id: 'advanced_smart_cart_recovery', icon: '⚡', title: __( 'Advanced Smart Cart Recovery (3-Way Branching)', 'ai-marketing-expert' ), badge: __( 'Max ROI', 'ai-marketing-expert' ), prompt: __( 'When a customer abandons their WooCommerce cart, branch on cart contents with 3-way smart recovery: 1) If cart_type equals duplicate_qty, send a 1-click single-quantity checkout email with {event.single_qty_url}; 2) If cart_type equals multiple_items, send an email offering individual item 1-click checkout links with {event.single_item_links}; 3) Otherwise for single items, send a standard recovery email with {event.recovery_url}.', 'ai-marketing-expert' ), }, { id: 'weekly_blog', icon: '✍️', title: __( 'Weekly Auto-Blogger & Social Blast', 'ai-marketing-expert' ), badge: __( 'Content & SEO', 'ai-marketing-expert' ), prompt: __( 'Every Tuesday at 9 AM, use AI Brain to select a fresh marketing topic, generate a 1500-word SEO blog post, run an SEO audit, and schedule a post on LinkedIn and Facebook.', 'ai-marketing-expert' ), }, { id: 'new_user', icon: '👤', title: __( 'New User Onboarding Drip', 'ai-marketing-expert' ), badge: __( 'Community & Growth', 'ai-marketing-expert' ), prompt: __( 'When a new user registers on WordPress, wait 5 minutes, send a warm welcome email introducing our top features, and enroll them in the onboarding funnel.', 'ai-marketing-expert' ), }, { id: 'order_upsell', icon: '🎉', title: __( 'Order Completed Review Request', 'ai-marketing-expert' ), badge: __( 'Retention', 'ai-marketing-expert' ), prompt: __( 'When a WooCommerce order is completed, wait 2 hours, then send a personalized thank you email asking for a product review and offering an exclusive discount on their next purchase.', 'ai-marketing-expert' ), }, { id: 'webhook_lead', icon: '🌐', title: __( 'Inbound Webhook Lead Capture', 'ai-marketing-expert' ), badge: __( 'Integrations', 'ai-marketing-expert' ), prompt: __( 'When an inbound webhook is received from an external form, enroll the lead into our email marketing funnel and send an email notification to the team.', 'ai-marketing-expert' ), }, { id: 'lead_followup', icon: '📩', title: __( 'Contact Form / Lead AI Follow-up', 'ai-marketing-expert' ), badge: __( 'Lead Capture', 'ai-marketing-expert' ), prompt: __( 'When a contact form is submitted, wait 5 minutes, send a warm personalized greeting email to the sender, and notify the sales team via email.', 'ai-marketing-expert' ), }, { id: 'post_social_blast', icon: '📢', title: __( 'New Post Auto-Social Blast', 'ai-marketing-expert' ), badge: __( 'Distribution', 'ai-marketing-expert' ), prompt: __( 'When a new WordPress post is published, wait 10 minutes, generate an SEO-optimized summary, and publish promotional posts on Facebook and LinkedIn.', 'ai-marketing-expert' ), }, { id: 'monthly_retention', icon: '🎯', title: __( 'Monthly AI Re-engagement', 'ai-marketing-expert' ), badge: __( 'Retention', 'ai-marketing-expert' ), prompt: __( 'On the 1st day of every month at 10 AM, use AI Brain to analyze top marketing ideas, create a re-engagement email campaign, and draft a blog post.', 'ai-marketing-expert' ), }, ]; const AiWorkflowModal = ( { open, onClose, onGenerate, brandVoices = [], } ) => { const [ prompt, setPrompt ] = useState( '' ); const [ brandVoiceId, setBrandVoiceId ] = useState( 0 ); const [ loading, setLoading ] = useState( false ); if ( ! open ) { return null; } const handleGenerate = async () => { const cleanPrompt = prompt.trim(); if ( ! cleanPrompt ) { toast( __( 'Please describe the workflow you want to generate.', 'ai-marketing-expert' ), 'warning' ); return; } setLoading( true ); try { const res = await apiPost( '/workflow-automation/ai-generate', { prompt: cleanPrompt, brand_voice_id: brandVoiceId, } ); if ( res?.workflow ) { onGenerate( res.workflow ); onClose(); } else { throw new Error( res?.message || __( 'Failed to generate workflow.', 'ai-marketing-expert' ) ); } } catch ( e ) { toast( e?.message || __( 'AI generation failed. Please try rephrasing your prompt.', 'ai-marketing-expert' ), 'error' ); } finally { setLoading( false ); } }; const applyRecipe = ( recipePrompt ) => { setPrompt( recipePrompt ); }; return (

{ __( 'Describe your automation goal in plain words (English or Bengali). AI will automatically select the best triggers, actions, and generate the full visual node graph on your canvas.', 'ai-marketing-expert' ) }

{ POPULAR_USE_CASES.map( ( item ) => ( ) ) }
💡 { __( 'Prompt Formula', 'ai-marketing-expert' ) } { __( '[When this happens...] ➔ [Wait/Delay (optional)] ➔ [Do this action]', 'ai-marketing-expert' ) }
{ __( 'Supported Triggers:', 'ai-marketing-expert' ) } { __( 'Schedules, Abandoned Carts, Orders, Signups, Comments, Forms & Webhooks.', 'ai-marketing-expert' ) }
{ __( 'Supported Actions:', 'ai-marketing-expert' ) } { __( 'AI Brain, SEO Articles, Social Posts, Funnels, Direct Email, Wait/Delay, Alerts.', 'ai-marketing-expert' ) }
ℹ️ { __( 'Tip: External services (e.g. Telegram, WhatsApp, SMS) will automatically adapt into Admin Email Notifications.', 'ai-marketing-expert' ) }
{ brandVoices.length > 0 && (
( { value: bv.id, label: bv.name, } ) ), ] } onChange={ ( val ) => setBrandVoiceId( Number( val ) ) } disabled={ loading } />
) } { loading && (
{ __( 'AI is architecting your workflow triggers, actions, and connections…', 'ai-marketing-expert' ) }
) }
{ loading ? ( { __( 'Generating Workflow…', 'ai-marketing-expert' ) } ) : ( )}
); }; export default AiWorkflowModal;