"use client"; import React, { useState, useEffect } from 'react'; import { Sparkles, X, Check, Copy, AlertCircle, Loader2, Cpu, ArrowRight, Wand2, MessageSquare, Shuffle } from 'lucide-react'; import { apiFetch } from '@/lib/auth'; import { evaluateSpintax } from './SpintaxModal'; interface TemplateAiAssistantModalProps { isOpen: boolean; onClose: () => void; onSelectVariation: (content: string, suggestedTitle?: string) => void; } const INSPIRATION_PROMPTS = [ { label: "IPTV Channel Promo", prompt: "I want a high-converting message to promote my premium IPTV channel. Highlight 4K buffer-free sports & cinema channels, offer a 24-hour instant free trial, and include channel link: t.me/yourchannel." }, { label: "Crypto VIP Alpha", prompt: "Write a high-converting invitation for our exclusive Crypto Signals & Alpha group. Emphasize verified 85% win rate, daily market breakdowns, and limited VIP seats. Channel link: t.me/yourchannel." }, { label: "B2B Outreach / Services", prompt: "Draft a personalized direct message offering our web design & automation agency services to business owners. Offer a free 15-minute growth audit with no strings attached." }, { label: "E-Commerce Deals", prompt: "Create an exciting promotion message for our discount deals community. Mention up to 70% off trending gadgets, verified seller discounts, and daily flash sales." } ]; const VARIATION_ANGLES = [ "Direct Value & Free Trial", "Curiosity & Exclusive VIP Hook", "Social Proof & Problem-Solver", "Conversational Soft Question", "High Urgency & Limited-Time Perk" ]; export function TemplateAiAssistantModal({ isOpen, onClose, onSelectVariation }: TemplateAiAssistantModalProps) { const [prompt, setPrompt] = useState(''); const [sampleText, setSampleText] = useState(''); const [showSampleInput, setShowSampleInput] = useState(false); const [includeSpintax, setIncludeSpintax] = useState(true); const [length, setLength] = useState<'short' | 'moderate' | 'lengthy'>('moderate'); const [isGenerating, setIsGenerating] = useState(false); const [variations, setVariations] = useState([]); const [error, setError] = useState(''); const [copiedIdx, setCopiedIdx] = useState(null); const [samplePreviews, setSamplePreviews] = useState>({}); const [configuredModel, setConfiguredModel] = useState(''); useEffect(() => { if (isOpen) { setError(''); try { const m = localStorage.getItem('arkitel_ai_model'); if (m) { const shortName = m.split('/')[1] || m; setConfiguredModel(shortName.replace(/-instruct|-chat|-versatile/g, '')); } else { setConfiguredModel('Llama 3.3 70B'); } } catch {} } }, [isOpen]); if (!isOpen) return null; const handleGenerate = async () => { if (!prompt.trim()) { setError('Please explain what message or offer you want to generate.'); return; } let key = ''; let model = 'meta-llama/llama-3.3-70b-instruct'; try { key = localStorage.getItem('arkitel_ai_key') || ''; model = localStorage.getItem('arkitel_ai_model') || 'meta-llama/llama-3.3-70b-instruct'; } catch {} if (!key) { setError('No OpenRouter AI key found. Please open Scraper > AI Provider to save your OpenRouter key first.'); return; } setIsGenerating(true); setError(''); try { const res = await apiFetch('/api/ai/generate-template-variations', { method: 'POST', body: JSON.stringify({ prompt: prompt.trim(), sample_text: sampleText.trim() || undefined, api_key: key, model: model, include_spintax: includeSpintax, count: 5, length: length }) }); const data = await res.json(); if (res.ok && Array.isArray(data.variations) && data.variations.length > 0) { setVariations(data.variations); setSamplePreviews({}); } else { const detailMsg = typeof data.detail === 'string' ? data.detail : Array.isArray(data.detail) ? data.detail.map((d: any) => d.msg || JSON.stringify(d)).join(', ') : 'Failed to generate message variations. Please try another prompt.'; setError(detailMsg); } } catch (e: any) { setError(e.message || 'Error communicating with AI assistant.'); } finally { setIsGenerating(false); } }; const handleCopy = (text: string, idx: number) => { navigator.clipboard.writeText(text); setCopiedIdx(idx); setTimeout(() => setCopiedIdx(null), 2000); }; const handleTogglePreviewSpin = (idx: number, spintaxText: string) => { setSamplePreviews(prev => ({ ...prev, [idx]: evaluateSpintax(spintaxText) })); }; return (
{/* Header */}

AI Copywriter & Template Generator {configuredModel && ( {configuredModel} )}

Describe your offer, niche, or channel to instantly generate 5 high-converting message variations.

{/* Body */}
{/* Input Section */}
{/* Word Volume / Length Selector */}
Volume: {(['short', 'moderate', 'lengthy'] as const).map((v) => ( ))}