#!/usr/bin/env python3
"""
Session 22 Swarm Launcher — 200 Agents (100 Kimi K2.5 + 100 MiniMax M2.5)
Sunaiva Talking Widget tasks across 10 work areas.

Each work area gets 10 Kimi agents (code/technical) + 10 MiniMax agents (content/marketing).
Total: 100 Kimi + 100 MiniMax = 200 agents.

Output: /mnt/e/genesis-system/swarm-output/session22/
"""

import os
import sys
import json
import asyncio
import time
from datetime import datetime
from dataclasses import dataclass, asdict
from typing import List, Optional
from pathlib import Path

try:
    import aiohttp
except ImportError:
    print("ERROR: aiohttp not installed. Install with: pip install aiohttp")
    sys.exit(1)

# Configuration
OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1/chat/completions"
OPENROUTER_API_KEY = "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"

MINIMAX_MODEL_ID = "minimax/minimax-m2.5"
KIMI_MODEL_ID = "moonshotai/kimi-k2.5"

BATCH_SIZE_MINIMAX = 20
BATCH_SIZE_KIMI = 5
REQUEST_TIMEOUT_SECONDS = 300
BATCH_TIMEOUT_SECONDS = 600

OUTPUT_DIR = "/mnt/e/genesis-system/swarm-output/session22"

# ============================================================
# STORY DEFINITIONS — 10 work areas x 10 stories each per model
# ============================================================

KIMI_SYSTEM_PROMPT = """You are an expert software engineer and technical architect working on Sunaiva Talking Widget — an AI voice widget for Australian tradie businesses.

Tech stack: Python FastAPI backend, vanilla JS widget (shadow DOM, Telnyx WebRTC), PostgreSQL, Redis, Qdrant.

Write production-ready code with type hints, error handling, docstrings, and tests. Follow PEP 8. Be thorough and complete — no placeholders or TODOs."""

MINIMAX_SYSTEM_PROMPT = """You are an expert content strategist, UX copywriter, and marketing specialist working on Sunaiva Talking Widget — an AI voice widget for Australian tradie businesses (plumbers, electricians, HVAC, builders, landscapers).

Target market: Australian small business owners, agency partners who resell to tradies.
Brand voice: Professional but approachable, Australian English, practical and results-focused.
Pricing: Starter $197/mo, Pro $397/mo, Growth $597/mo. Agency commission 30%.

Create compelling, accurate, production-ready content. No fluff. Every word must earn its place."""


def generate_stories():
    """Generate 200 stories: 100 Kimi (technical) + 100 MiniMax (content)."""
    stories = []
    story_id = 1

    # ============================================================
    # AREA 1: Widget.js Telnyx Reference Obfuscation (20 agents)
    # ============================================================
    area1_kimi = [
        ("TRO-K01", "Audit all 58 Telnyx references in widget.js", "Scan the entire widget.js codebase and produce a complete inventory of all 58 Telnyx references (variable names, function names, class names, string literals, comments, CDN URLs). Output a JSON manifest with line numbers, reference types, and suggested generic replacements."),
        ("TRO-K02", "Create Telnyx-to-generic name mapping module", "Build a Python module that defines the complete mapping from Telnyx-specific names to generic equivalents. E.g. TelnyxRTC -> VoiceEngine, telnyx-webrtc -> voice-core. Include reverse mapping for debugging. Export as JSON and Python dict."),
        ("TRO-K03", "Build automated find-and-replace obfuscation script", "Write a Python script that reads widget.js, applies the name mapping from TRO-K02, and produces an obfuscated version. Must handle: variable names, function names, class references, string literals, CDN URLs, and comments. Include --dry-run mode."),
        ("TRO-K04", "Obfuscate WebRTC initialization code", "Refactor the WebRTC initialization section to use generic names (VoiceSession, MediaBridge, SignalChannel) instead of Telnyx-specific names. Maintain identical functionality. Include before/after diff."),
        ("TRO-K05", "Obfuscate event handler names and callbacks", "Rename all Telnyx-specific event handlers (telnyx.ready, telnyx.error, telnyx.notification etc.) to generic equivalents (voice.ready, voice.error, voice.notification). Produce the refactored event handling code."),
        ("TRO-K06", "Create CDN URL abstraction layer", "Build a URL abstraction that replaces direct Telnyx CDN references with a configurable endpoint. The widget should load its voice SDK from a URL specified in widget config, defaulting to a proxied path. Write the loader code."),
        ("TRO-K07", "Build obfuscation verification test suite", "Write a comprehensive test suite (pytest) that: 1) Verifies no Telnyx strings remain in obfuscated output, 2) Checks all function signatures still match, 3) Validates WebRTC flow still works with mocked connections, 4) Tests CDN URL abstraction."),
        ("TRO-K08", "Create source map for debugging obfuscated code", "Build a source map generator that maps obfuscated names back to originals for debugging. Output a .map file compatible with browser devtools. Include a debug mode flag that loads the source map."),
        ("TRO-K09", "Obfuscate error messages and logging strings", "Find all error messages, console.log statements, and user-facing strings that mention Telnyx. Replace with generic equivalents. Produce the complete list of string replacements and the refactored logging code."),
        ("TRO-K10", "Build CI/CD obfuscation pipeline step", "Write a GitHub Actions workflow step and a standalone shell script that runs the obfuscation as part of the build pipeline. Input: raw widget.js. Output: obfuscated widget.min.js + source map. Include checksum verification."),
    ]

    area1_minimax = [
        ("TRO-M01", "Developer documentation for obfuscation process", "Write comprehensive developer documentation explaining: why we obfuscate Telnyx references, how the mapping works, how to add new references, how to debug using source maps, and the CI/CD integration. Markdown format."),
        ("TRO-M02", "Obfuscation naming convention guide", "Create a naming convention guide for generic replacements. Define patterns for: SDK names, event names, config keys, error codes, CSS classes. Ensure consistency across all 58 references. Include rationale for each choice."),
        ("TRO-M03", "Before/after comparison document", "Create a side-by-side comparison document showing every Telnyx reference and its generic replacement. Format as a table with columns: Original, Replacement, Type, Location, Risk Level. Export as MD and CSV."),
        ("TRO-M04", "Risk assessment for obfuscation", "Write a risk assessment covering: what breaks if obfuscation is incomplete, how to detect leaks, legal considerations of hiding vendor names, impact on debugging, impact on Telnyx support interactions. Include mitigation strategies."),
        ("TRO-M05", "Partner-facing technical brief on voice technology", "Write a 1-page technical brief for agency partners explaining the voice technology without mentioning Telnyx. Use terms like 'enterprise-grade WebRTC infrastructure' and 'carrier-grade voice network'. Professional but accessible tone."),
        ("TRO-M06", "Customer-facing FAQ about voice quality and reliability", "Write 10 FAQ entries about voice quality, uptime, call reliability, and supported browsers — all without mentioning Telnyx by name. Use terms like 'our voice infrastructure partner' or 'enterprise telecom backbone'."),
        ("TRO-M07", "Internal changelog for obfuscation release", "Write a detailed internal changelog documenting the obfuscation changes, what was renamed, potential breakage points, rollback procedures, and testing checklist. For the engineering team."),
        ("TRO-M08", "Security review checklist for obfuscated widget", "Create a security review checklist ensuring obfuscation doesn't introduce vulnerabilities: no hardcoded credentials exposed, no debug endpoints left open, CSP headers updated, subresource integrity hashes recalculated."),
        ("TRO-M09", "Widget.js code comments and JSDoc for obfuscated version", "Write comprehensive JSDoc comments for the obfuscated widget.js using only generic names. Document every public function, event, configuration option, and callback. Production-quality inline documentation."),
        ("TRO-M10", "Obfuscation maintenance runbook", "Write an operations runbook for maintaining the obfuscation: how to update when Telnyx releases new SDK versions, how to add new references to the mapping, how to verify no leaks, monthly audit procedure."),
    ]

    # ============================================================
    # AREA 2: Mobile-Responsive Testing Checklist (20 agents)
    # ============================================================
    area2_kimi = [
        ("MRT-K01", "Playwright mobile viewport test suite", "Write a complete Playwright test suite testing the widget across 10 mobile viewports (iPhone SE, iPhone 14, iPhone 14 Pro Max, Samsung Galaxy S23, Pixel 7, iPad Mini, iPad Pro, Galaxy Tab, Surface Pro, Galaxy Fold). Test: widget opens, resizes correctly, buttons are tappable, text is readable."),
        ("MRT-K02", "Touch event handler tests", "Write tests verifying all touch interactions work: tap to open widget, swipe to dismiss, pinch-to-zoom disabled inside widget, long-press on phone numbers, scroll within widget conversation area. Include touch event simulation code."),
        ("MRT-K03", "CSS media query validation suite", "Write a test suite that validates all CSS media queries in the widget fire correctly at their breakpoints. Test: font sizes change, layout shifts from desktop to mobile, button sizes meet 44px minimum touch target, padding adjusts."),
        ("MRT-K04", "Orientation change handling tests", "Write tests for portrait-to-landscape and landscape-to-portrait transitions. Verify: widget repositions correctly, no content overflow, active voice call survives orientation change, keyboard doesn't break layout."),
        ("MRT-K05", "Mobile keyboard interaction tests", "Write tests for virtual keyboard behavior: widget input field focuses correctly, widget scrolls to keep input visible above keyboard, keyboard dismiss restores layout, autocorrect/autocomplete compatibility."),
        ("MRT-K06", "Mobile performance benchmark script", "Write a performance benchmark that measures on mobile viewports: time to first paint, time to interactive, JavaScript bundle parse time, memory usage during voice call, battery impact estimation. Output as JSON report."),
        ("MRT-K07", "Accessibility audit for mobile widget", "Write an automated accessibility test suite: screen reader compatibility (VoiceOver/TalkBack), focus order is logical, ARIA labels present, color contrast meets WCAG 2.1 AA on mobile, dynamic content announced."),
        ("MRT-K08", "Network condition simulation tests", "Write tests simulating mobile network conditions: 3G slow, 4G, 5G, offline/online transitions, packet loss during voice calls. Verify: graceful degradation, reconnection logic, user-facing error messages."),
        ("MRT-K09", "Mobile browser compatibility matrix test", "Write a test runner that produces a compatibility matrix for: Chrome Mobile, Safari iOS, Firefox Mobile, Samsung Internet, Opera Mobile, Edge Mobile. Test core widget functions on each. Output pass/fail matrix."),
        ("MRT-K10", "Widget shadow DOM mobile rendering tests", "Write tests verifying the shadow DOM encapsulation works on mobile: host page CSS doesn't leak in, widget CSS doesn't leak out, fonts load correctly in shadow DOM, animations perform smoothly on mobile GPU."),
    ]

    area2_minimax = [
        ("MRT-M01", "Complete mobile testing checklist document", "Create a comprehensive mobile testing checklist (50+ items) covering: viewports, touch interactions, orientation, keyboard, performance, accessibility, network conditions, browser compatibility. Format as a checkable markdown list."),
        ("MRT-M02", "Device priority matrix for QA", "Create a prioritized device testing matrix showing which devices to test first based on Australian market share data. Include: device name, OS version, browser, screen size, market share %, priority tier (P0/P1/P2)."),
        ("MRT-M03", "Mobile UX guidelines for widget", "Write mobile UX guidelines covering: minimum touch targets (44px), font sizes for mobile readability, widget position (bottom-right vs bottom-center on mobile), animation performance budgets, gesture expectations."),
        ("MRT-M04", "Bug report template for mobile issues", "Create a structured bug report template specifically for mobile widget issues. Include fields: device, OS, browser, viewport size, network condition, steps to reproduce, expected vs actual, screenshot/video requirement."),
        ("MRT-M05", "Mobile testing environment setup guide", "Write a step-by-step guide for setting up mobile testing environments: BrowserStack configuration, local device lab setup, Chrome DevTools device mode tips, real device vs emulator trade-offs, recommended test devices to purchase."),
        ("MRT-M06", "Responsive design specifications for widget", "Create a responsive design specification document: breakpoints (320px, 375px, 414px, 768px, 1024px, 1440px), layout changes at each breakpoint, component behavior changes, font scale, spacing scale, image sizing."),
        ("MRT-M07", "Mobile performance budget document", "Define a performance budget for the mobile widget: max JS bundle size (target <50KB gzipped), max time to interactive (<3s on 4G), max memory usage, max CPU usage during idle, max battery drain per hour."),
        ("MRT-M08", "QA sign-off criteria for mobile release", "Create QA sign-off criteria that must pass before any mobile release: minimum devices tested, minimum viewports verified, accessibility audit score, performance budget adherence, regression test pass rate."),
        ("MRT-M09", "Mobile A/B test plan for widget placement", "Create an A/B test plan for mobile widget placement: bottom-right corner vs bottom-center, floating vs fixed, auto-open vs manual, size variations. Define metrics: engagement rate, voice call initiation rate, bounce impact."),
        ("MRT-M10", "Known mobile issues and workarounds document", "Create a living document template for tracking known mobile issues and workarounds. Pre-populate with 10 common WebRTC mobile issues: iOS Safari autoplay restrictions, Android Chrome permission prompts, Samsung Internet WebRTC quirks, etc."),
    ]

    # ============================================================
    # AREA 3: SEO Meta Tags and Open Graph Tags (20 agents)
    # ============================================================
    area3_kimi = [
        ("SEO-K01", "Meta tag generator module for all landing pages", "Build a Python module that generates complete HTML meta tags for any Sunaiva landing page. Input: page title, description, URL, image. Output: complete <head> section with title, description, canonical, robots, viewport, charset, and all Open Graph tags."),
        ("SEO-K02", "Twitter Card meta tag generator", "Extend the meta tag generator with Twitter Card support. Generate: twitter:card (summary_large_image), twitter:site, twitter:creator, twitter:title, twitter:description, twitter:image. Include validation for image dimensions."),
        ("SEO-K03", "JSON-LD structured data for SaaS product", "Generate complete JSON-LD structured data for: SoftwareApplication (the widget product), Organization (Sunaiva), FAQPage, HowTo, and BreadcrumbList schemas. All valid per schema.org specs."),
        ("SEO-K04", "Dynamic sitemap generator", "Build a Python script that generates sitemap.xml for Sunaiva's landing pages. Include: homepage, pricing, features, FAQ, blog index, individual blog posts. Support lastmod, changefreq, priority. Output valid XML."),
        ("SEO-K05", "Robots.txt and crawl configuration", "Generate an optimized robots.txt file for Sunaiva. Include: allow/disallow rules, sitemap reference, crawl-delay for aggressive bots, block AI training crawlers (GPTBot, CCBot, Google-Extended), allow Googlebot and Bingbot."),
        ("SEO-K06", "Page speed optimization meta tags", "Generate all performance-related meta tags and link elements: preconnect to CDNs, dns-prefetch, preload critical resources (fonts, hero image, widget JS), modulepreload for ES modules. Include resource hints."),
        ("SEO-K07", "Hreflang tags for AU/US/UK markets", "Generate hreflang tags for targeting Australian, US, and UK English markets. Include x-default fallback. Build a function that generates correct hreflang for any page URL across all three markets."),
        ("SEO-K08", "SEO audit script for landing pages", "Build a Python script that audits any HTML page for SEO completeness. Check: title length (50-60 chars), meta description (150-160 chars), H1 presence, image alt tags, canonical URL, OG tags, JSON-LD validity. Output score and recommendations."),
        ("SEO-K09", "Canonical URL management system", "Build a canonical URL management module that: generates correct canonical URLs, handles trailing slashes consistently, manages www vs non-www, handles pagination canonicals, prevents duplicate content issues."),
        ("SEO-K10", "SEO-friendly URL slug generator", "Build a URL slug generator for Sunaiva's blog and landing pages. Handle: Australian English spelling, special characters, length limits (50-75 chars), keyword preservation, collision detection, redirect mapping from old slugs."),
    ]

    area3_minimax = [
        ("SEO-M01", "Meta descriptions for all 10 core landing pages", "Write optimized meta descriptions (150-160 chars each) for: homepage, pricing, features, how-it-works, FAQ, about, contact, partner program, case studies, blog. Each must include primary keyword and compelling CTA."),
        ("SEO-M02", "Open Graph content for social sharing", "Write OG titles (60 chars), descriptions (200 chars), and image alt text for all core pages. Optimize for clicks when shared on Facebook, LinkedIn, and Twitter. Include recommended image dimensions and templates."),
        ("SEO-M03", "Primary and secondary keyword map", "Create a keyword map for Sunaiva Talking Widget: primary keyword per page, secondary keywords (3-5), long-tail variations, search intent classification (informational/commercial/transactional), estimated search volume (AU market)."),
        ("SEO-M04", "Title tag templates for all page types", "Create optimized title tags (50-60 chars) for: landing pages, blog posts, case studies, help articles, pricing page, comparison pages. Include brand name positioning (front vs back), separator choices, and keyword placement rules."),
        ("SEO-M05", "Image alt text library for marketing assets", "Write descriptive, keyword-rich alt text for 20 common marketing images: hero banner, widget screenshot, dashboard screenshot, mobile widget, partner logo grid, testimonial headshots, feature icons, comparison charts."),
        ("SEO-M06", "Blog post SEO brief template", "Create a reusable SEO brief template for Sunaiva blog posts. Include: target keyword, search intent, recommended word count, H2/H3 structure, internal linking targets, external reference sources, meta description draft, featured snippet opportunity."),
        ("SEO-M07", "Local SEO optimization for Australian market", "Write local SEO strategy content: Google Business Profile description, local schema markup content, city-specific landing page templates (Sydney, Melbourne, Brisbane, Perth, Adelaide), NAP consistency guidelines."),
        ("SEO-M08", "Competitor SEO gap analysis template", "Create a competitor SEO gap analysis template. Pre-fill with analysis framework for comparing Sunaiva vs generic chatbots vs live chat services. Include: keyword gaps, content gaps, backlink opportunities, featured snippet targets."),
        ("SEO-M09", "FAQ schema content for featured snippets", "Write 20 FAQ entries optimized for Google featured snippets. Topics: AI voice widgets, tradie business automation, website chat alternatives, voice AI for small business, Australian business phone systems. Each answer 40-60 words."),
        ("SEO-M10", "SEO launch checklist for new landing pages", "Create a comprehensive SEO launch checklist for any new Sunaiva landing page. 30+ items covering: on-page SEO, technical SEO, content quality, internal linking, schema markup, social sharing, analytics setup, Search Console submission."),
    ]

    # ============================================================
    # AREA 4: Customer Onboarding Email Templates (20 agents)
    # ============================================================
    area4_kimi = [
        ("OBE-K01", "Email template rendering engine", "Build a Python email template engine using Jinja2 that renders Sunaiva onboarding emails. Support: variable interpolation (customer name, plan, widget URL), conditional blocks (plan-specific content), responsive HTML/CSS, plain text fallback."),
        ("OBE-K02", "Welcome email HTML template with responsive design", "Build a production-ready responsive HTML email template for the welcome email. Must work in: Gmail, Outlook, Apple Mail, Yahoo. Use table-based layout, inline CSS, dark mode support. Include Sunaiva branding."),
        ("OBE-K03", "Email delivery integration with Resend API", "Build a Python module that sends onboarding emails via Resend API. Include: template selection, variable substitution, send scheduling, delivery tracking, bounce/complaint handling, retry logic with exponential backoff."),
        ("OBE-K04", "Onboarding drip sequence scheduler", "Build a drip email scheduler that sends the 5-email onboarding sequence on a schedule: Day 0 (welcome), Day 1 (setup guide), Day 3 (first week tips), Day 7 (check-in), Day 14 (optimization tips). Use Redis for job queue."),
        ("OBE-K05", "Email analytics tracking module", "Build email analytics tracking: open rates (tracking pixel), click rates (link wrapping), unsubscribe handling, bounce processing. Store metrics in PostgreSQL. Include dashboard query functions."),
        ("OBE-K06", "A/B testing framework for email subject lines", "Build an A/B testing framework for email subject lines. Split recipients randomly, track open rates per variant, determine statistical significance, auto-select winner after sample size reached. Include test management API."),
        ("OBE-K07", "Email preference center backend", "Build a preference center API: manage email frequency, opt-in/out of email types (marketing, product updates, tips), save timezone for send time optimization, GDPR-compliant preference storage, one-click unsubscribe."),
        ("OBE-K08", "Automated setup completion detector", "Build a module that detects when a customer has completed key setup steps (widget installed, first call received, business hours configured) and triggers the appropriate next email in the sequence. Event-driven architecture."),
        ("OBE-K09", "Email template unit tests", "Write comprehensive tests for the email system: template rendering (all variables, edge cases), Resend API integration (mock), scheduler timing, analytics tracking, A/B test logic, preference center, setup detection triggers."),
        ("OBE-K10", "Email bounce and complaint handler", "Build a webhook handler for Resend's bounce and complaint webhooks. Auto-suppress bounced addresses, flag complaint addresses, update customer records, alert on high bounce rates, implement re-engagement flow for soft bounces."),
    ]

    area4_minimax = [
        ("OBE-M01", "Welcome email copy — warm, exciting, actionable", "Write the welcome email (subject + body) for new Sunaiva customers. Tone: warm, exciting, professional. Include: personal greeting, what they just got access to, 3 quick-start steps, support contact, expected next email timing. 200-300 words."),
        ("OBE-M02", "Setup guide email — step-by-step widget installation", "Write the Day 1 setup guide email. Walk through: copying the widget embed code, pasting into their website, configuring business hours, recording a greeting, testing the first call. Include troubleshooting tips. 300-400 words."),
        ("OBE-M03", "First week tips email — getting the most value", "Write the Day 3 first week tips email. Cover: how to review call transcripts, setting up lead notifications, customizing the AI's personality for their trade, connecting to their CRM, tips from other tradies. 250-350 words."),
        ("OBE-M04", "Check-in email — Day 7 engagement", "Write the Day 7 check-in email. Ask how setup went, offer help if they haven't installed yet, share a quick win stat ('businesses like yours capture 12 more leads/month'), link to book a setup call, introduce the partner program."),
        ("OBE-M05", "Optimization tips email — Day 14 power user", "Write the Day 14 optimization email. Advanced tips: call routing by time of day, holiday messages, after-hours scripts, integrating with booking systems, reading analytics to improve conversion, upgrading plan benefits."),
        ("OBE-M06", "Subject line variants for A/B testing — all 5 emails", "Write 3 subject line variants for each of the 5 onboarding emails (15 total). Each variant should test a different psychological trigger: curiosity, urgency, social proof, personalization, benefit-focused. Max 50 chars each."),
        ("OBE-M07", "Plain text versions of all 5 emails", "Write plain text versions of all 5 onboarding emails. These are fallbacks for email clients that don't render HTML. Must be equally compelling without formatting. Include ASCII-art section dividers."),
        ("OBE-M08", "Re-engagement email for inactive signups", "Write a re-engagement email for customers who signed up but never installed the widget (Day 21). Tone: helpful not pushy. Offer: free setup assistance, quick video walkthrough, deadline for beta pricing. Include 'pause subscription' option."),
        ("OBE-M09", "Referral program introduction email", "Write an email introducing the referral program. Explain: refer a tradie mate, both get 1 month free. Simple sharing link. Social proof: 'Join 50+ tradies already using Sunaiva'. CTA: share your unique referral link."),
        ("OBE-M10", "Email footer and legal copy for Australian compliance", "Write email footer content compliant with Australian Spam Act 2003: company name and ABN, physical address, clear unsubscribe mechanism, privacy policy link, contact details. Also write the unsubscribe confirmation page copy."),
    ]

    # ============================================================
    # AREA 5: FAQ Content for Tradie Widget (20 agents)
    # ============================================================
    area5_kimi = [
        ("FAQ-K01", "FAQ page component with search and filtering", "Build a React-free vanilla JS FAQ component with: search bar that filters questions in real-time, category tabs (General, Setup, Billing, Technical, Voice), smooth accordion open/close animations, deep-linking to specific questions via URL hash."),
        ("FAQ-K02", "FAQ content management API", "Build a FastAPI endpoint for managing FAQ content: CRUD operations, category management, ordering, published/draft status, view count tracking, helpful/not-helpful voting, search endpoint with relevance ranking."),
        ("FAQ-K03", "FAQ schema.org structured data generator", "Build a function that takes FAQ entries and generates valid FAQPage schema.org JSON-LD. Include: question, acceptedAnswer, dateCreated, dateModified. Validate output against Google's rich results test requirements."),
        ("FAQ-K04", "AI-powered FAQ suggestion engine", "Build a module that analyzes customer support tickets and voice call transcripts to suggest new FAQ entries. Use TF-IDF to identify common question patterns, cluster similar questions, suggest answers from existing knowledge base."),
        ("FAQ-K05", "FAQ analytics and reporting", "Build FAQ analytics: track which questions are viewed most, which answers get 'not helpful' votes, search queries with no results (content gaps), time-on-page per FAQ entry. Store in PostgreSQL, expose via API."),
        ("FAQ-K06", "Multilingual FAQ framework", "Build a framework for serving FAQs in multiple languages. Start with Australian English, support adding US English and other languages. Include: translation key management, language detection, fallback to English, RTL support structure."),
        ("FAQ-K07", "FAQ chatbot integration module", "Build a module that connects the FAQ knowledge base to the voice widget's AI. When a customer asks a common question during a voice call, the AI can pull the answer from the FAQ database for consistent, accurate responses."),
        ("FAQ-K08", "FAQ version history and audit trail", "Build version tracking for FAQ entries: who edited, when, what changed (diff), ability to rollback. Implement as an append-only audit log in PostgreSQL. Include API for viewing history and restoring previous versions."),
        ("FAQ-K09", "FAQ import/export tools", "Build import/export tools: import from CSV/JSON/Markdown, export to JSON/CSV/HTML/PDF. Support bulk operations. Include validation (no duplicate questions, answer length limits, category validation). Idempotent imports."),
        ("FAQ-K10", "FAQ caching and performance optimization", "Build a Redis-based caching layer for FAQ content. Cache: full FAQ list, category-filtered lists, search results, individual entries. Include: cache invalidation on edit, warm cache on deploy, cache hit/miss metrics."),
    ]

    area5_minimax = [
        ("FAQ-M01", "General FAQs — 15 questions about Sunaiva", "Write 15 general FAQ entries: What is Sunaiva Talking Widget? How does it work? Is it really AI? What trades does it work for? How quickly can I set it up? Do I need technical skills? Does it work on mobile? What happens if the AI can't answer? etc."),
        ("FAQ-M02", "Setup and installation FAQs — 10 questions", "Write 10 setup FAQ entries: How do I install the widget? Where do I put the code? Does it work with WordPress/Squarespace/Wix/Shopify? How do I customize the widget colors? How do I set business hours? How do I test it before going live?"),
        ("FAQ-M03", "Billing and pricing FAQs — 10 questions", "Write 10 billing FAQ entries: How much does it cost? Is there a free trial? What's included in each plan? How does billing work? Can I change plans? What happens if I cancel? Are there any hidden fees? What payment methods do you accept?"),
        ("FAQ-M04", "Technical FAQs — 10 questions", "Write 10 technical FAQ entries: What browsers are supported? Does it affect my website speed? How secure are voice calls? Is data stored in Australia? What's the uptime guarantee? Can I use my own phone number? Does it work with my CRM?"),
        ("FAQ-M05", "Voice quality and AI capability FAQs — 10 questions", "Write 10 voice/AI FAQ entries: How natural does the AI sound? Can callers tell it's AI? What accent does it use? Can it handle complex questions? What if someone speaks another language? How does it learn my business? Can it book appointments?"),
        ("FAQ-M06", "Tradie-specific FAQs — plumbing focus", "Write 10 FAQ entries specifically for plumbers: Can it quote on blocked drains? Does it know plumbing terminology? Can it triage emergency vs routine calls? How does it handle after-hours calls? Can it dispatch my team? Integration with ServiceM8/Tradify?"),
        ("FAQ-M07", "Tradie-specific FAQs — electrician focus", "Write 10 FAQ entries for electricians: Does it understand electrical terminology? Can it assess urgency (safety vs convenience)? Can it check if the job needs a licensed electrician? How does it handle Level 2 ASP referrals?"),
        ("FAQ-M08", "Tradie-specific FAQs — HVAC and builder focus", "Write 10 FAQ entries for HVAC techs and builders: Can it schedule HVAC maintenance? Does it handle seasonal demand spikes? Can it qualify commercial vs residential jobs? How does it handle builder project inquiries vs small repairs?"),
        ("FAQ-M09", "Agency partner FAQs — 10 questions", "Write 10 FAQ entries for agency partners: How does the partner program work? What's the commission structure? How do I white-label the widget? Can I manage multiple client widgets? What training/support do partners get? Minimum client requirements?"),
        ("FAQ-M10", "Comparison FAQs — vs competitors", "Write 10 comparison FAQ entries: How is this different from a chatbot? Why not just use live chat? How does it compare to a virtual receptionist service? Is it better than a phone answering service? Why AI voice vs text-only? Cost comparison vs hiring a receptionist."),
    ]

    # ============================================================
    # AREA 6: Testimonial/Social Proof Copy (20 agents)
    # ============================================================
    area6_kimi = [
        ("TSP-K01", "Testimonial display component with animations", "Build a vanilla JS testimonial carousel/grid component: auto-rotating display, star ratings, customer photo placeholders, trade type badges, verified customer checkmarks. Shadow DOM encapsulated. Responsive."),
        ("TSP-K02", "Social proof notification popup system", "Build a social proof notification system showing recent activity: 'Mike from Sydney just installed the widget 5 min ago', 'Sarah's widget handled 12 calls today'. Configurable frequency, dismissible, non-intrusive positioning."),
        ("TSP-K03", "Review aggregation and display API", "Build a FastAPI endpoint that aggregates reviews from multiple sources (Google, Facebook, internal), calculates aggregate rating, serves reviews with filtering (by trade, by rating, by recency). Include caching."),
        ("TSP-K04", "Testimonial collection automation", "Build an automated testimonial collection system: trigger email after 30 days of usage, include pre-filled template based on their stats, submit to review pipeline, auto-request Google review with proper link, track conversion."),
        ("TSP-K05", "Trust badge generator", "Build a module that generates trust badges based on real data: '500+ tradies trust Sunaiva', '50,000+ calls handled', '4.8 average rating', '99.9% uptime'. Badges update automatically from database stats. Output as SVG and PNG."),
        ("TSP-K06", "Case study template renderer", "Build a case study template engine: input customer data (name, trade, location, metrics), output a formatted case study page (HTML) and PDF. Include: hero image, challenge/solution/result sections, pull quotes, metric callouts."),
        ("TSP-K07", "Video testimonial embed component", "Build a video testimonial component: lazy-loaded YouTube/Vimeo embeds, custom play button overlay with customer photo and quote preview, responsive sizing, tracking (play starts, 25/50/75/100% watched)."),
        ("TSP-K08", "Social proof A/B testing framework", "Build an A/B testing framework specifically for social proof elements: test different testimonial placements, notification frequencies, badge styles. Track impact on conversion rate. Statistical significance calculator included."),
        ("TSP-K09", "Real-time stats counter animation", "Build an animated counter component that displays live stats: total calls handled, active businesses, customer satisfaction score, leads captured. Numbers animate up on scroll-into-view. Updates periodically via API."),
        ("TSP-K10", "Review response automation", "Build an automated review response system: detect sentiment of reviews, generate appropriate response templates (positive: thank + share, neutral: acknowledge + improve, negative: empathize + resolve), queue for human approval."),
    ]

    area6_minimax = [
        ("TSP-M01", "10 realistic testimonial quotes from tradies", "Write 10 realistic testimonial quotes from fictional Australian tradies. Include: name, trade, city, quote (2-3 sentences), specific metric they achieved. Trades: plumber, electrician, builder, landscaper, HVAC, painter, roofer, tiler, carpenter, locksmith."),
        ("TSP-M02", "3 detailed case studies — before/after", "Write 3 detailed case studies (500 words each): 1) Plumber in Melbourne who went from missing 40% of calls to capturing 95% of leads, 2) Electrician in Sydney who saved $3K/month vs receptionist, 3) Builder in Brisbane who doubled inquiry conversions."),
        ("TSP-M03", "Social proof statistics copy for landing page", "Write compelling social proof statistics sections: '500+ tradies trust Sunaiva', 'Handles 10,000+ calls monthly', '95% caller satisfaction', '$2.3M in leads captured for our customers'. Include context sentences for each stat."),
        ("TSP-M04", "Trust signals and credibility copy", "Write trust signal copy: 'Enterprise-grade security', 'Australian-owned and operated', 'Data stored in AU', '99.9% uptime SLA', 'No lock-in contracts', 'Cancel anytime', '14-day money-back guarantee'. One-liner + supporting paragraph for each."),
        ("TSP-M05", "Partner agency testimonials — 5 quotes", "Write 5 testimonials from fictional agency partners. Focus on: recurring revenue, client satisfaction, ease of white-labeling, support quality, competitive advantage. Include agency name, partner name, role, city."),
        ("TSP-M06", "Before/after comparison narratives", "Write 5 before/after comparison narratives showing life without vs with Sunaiva. Format: 'Before: [pain]. After: [transformation]. Result: [metric].' Cover: missed calls, after-hours inquiries, admin overhead, lead response time, customer experience."),
        ("TSP-M07", "Industry awards and recognition copy", "Write aspirational but honest awards section copy. Create badges for: 'Best AI Voice Solution 2026', 'Top 10 Business Tools for Tradies', 'Innovation Award — Australian Small Business'. Include disclaimers if not yet earned, or frame as goals."),
        ("TSP-M08", "Customer success metrics dashboard copy", "Write the copy for a customer success metrics section: 'Average response time: 3 seconds', 'Calls answered 24/7: 100%', 'Lead capture rate: 95%', 'Customer ROI: 8.5x'. Include explanatory tooltips and fine print."),
        ("TSP-M09", "Logo wall and partnership copy", "Write copy for the 'Trusted by' logo wall section. Create fictional but realistic business names for 12 tradie businesses across Australia. Include a 'Join 500+ Australian tradies' CTA. Write the section header and subtext."),
        ("TSP-M10", "Video testimonial scripts — 3 scripts", "Write 3 video testimonial scripts (60 seconds each) for: 1) A plumber who uses Sunaiva (natural, conversational), 2) An agency partner who resells it (business-focused), 3) A customer who called a Sunaiva-powered business (caller perspective)."),
    ]

    # ============================================================
    # AREA 7: API Documentation for Partner Integrations (20 agents)
    # ============================================================
    area7_kimi = [
        ("API-K01", "OpenAPI 3.0 spec for Partner API", "Write a complete OpenAPI 3.0 specification for the Sunaiva Partner API. Include: authentication (API key + OAuth2), endpoints for widget management, lead retrieval, analytics, billing, webhooks. Full request/response schemas with examples."),
        ("API-K02", "Partner API authentication module", "Build the authentication module for the Partner API: API key generation and validation, OAuth2 client credentials flow, rate limiting per API key (1000 req/hr), scope-based permissions (read, write, admin), token refresh logic."),
        ("API-K03", "Webhook delivery system for partners", "Build a webhook delivery system: event registration (call.completed, lead.captured, widget.installed), payload signing (HMAC-SHA256), retry logic (3 attempts with exponential backoff), delivery logging, webhook testing endpoint."),
        ("API-K04", "Partner SDK — Python client library", "Build a Python client library for the Partner API: Pythonic interface, async support, automatic retry, pagination handling, type hints throughout, comprehensive docstrings. Include: WidgetClient, LeadClient, AnalyticsClient, WebhookClient."),
        ("API-K05", "Partner SDK — JavaScript/Node.js client", "Build a JavaScript client library for the Partner API: TypeScript types, Promise-based, automatic retry, pagination cursors, webhook signature verification helper. Include: SunaivaClient class with all API methods."),
        ("API-K06", "API versioning and deprecation system", "Build API versioning infrastructure: URL path versioning (/v1/, /v2/), version negotiation, deprecation headers (Sunset, Deprecation), migration guides auto-generation, backward-compatible change detection."),
        ("API-K07", "Rate limiting and quota management", "Build a comprehensive rate limiting system: per-endpoint limits, per-API-key limits, burst allowances, quota tracking (monthly API calls per plan), rate limit headers (X-RateLimit-*), 429 response with retry-after."),
        ("API-K08", "API request/response logging and analytics", "Build API analytics: log all requests (method, endpoint, status, latency, client), aggregate metrics (requests/min, error rates, P50/P95/P99 latency), popular endpoints report, per-partner usage dashboard data."),
        ("API-K09", "Partner sandbox environment", "Build a sandbox environment for partners to test integrations: separate API keys for sandbox, pre-seeded test data, simulated webhook events, no billing impact, sandbox-specific rate limits, clear sandbox/production separation."),
        ("API-K10", "API health check and status page endpoints", "Build health check endpoints: /health (basic), /health/detailed (DB, Redis, voice service, external APIs), /status (current incidents, maintenance windows). Include: status page JSON feed, uptime calculation, incident history."),
    ]

    area7_minimax = [
        ("API-M01", "Partner API getting started guide", "Write a comprehensive Getting Started guide for the Partner API. Cover: obtaining API keys, making your first request, authentication, common workflows (create widget, retrieve leads, set up webhooks), error handling, rate limits."),
        ("API-M02", "API reference documentation — all endpoints", "Write detailed API reference docs for 15 core endpoints: list/create/update/delete widgets, list/export leads, get analytics, manage webhooks, partner account info, billing summary. Include request/response examples for each."),
        ("API-M03", "Webhook integration guide", "Write a webhook integration guide: what events are available, payload formats, signature verification (with code examples in Python, JavaScript, PHP), retry behavior, testing webhooks locally with ngrok, common pitfalls."),
        ("API-M04", "Partner onboarding documentation", "Write the partner onboarding documentation: account setup, API key generation, sandbox testing, first widget deployment, go-live checklist, billing setup, support escalation paths. Include a 'first 30 minutes' quickstart."),
        ("API-M05", "API error codes reference", "Write a complete error code reference: every error code (400-599), human-readable description, common causes, recommended fixes, example error responses. Format as a searchable reference table. Include retry guidance per error type."),
        ("API-M06", "Integration cookbook — common recipes", "Write an integration cookbook with 10 common recipes: 1) Embed widget on WordPress, 2) Sync leads to HubSpot, 3) Send lead alerts to Slack, 4) Build a client dashboard, 5) Automate widget provisioning, 6-10) more practical integrations."),
        ("API-M07", "API changelog template and first entry", "Create an API changelog format and write the first entry (v1.0.0 launch). Include: version number, date, breaking changes, new features, bug fixes, deprecations, migration guide. Establish the format for all future entries."),
        ("API-M08", "Security best practices for API consumers", "Write security best practices documentation: API key storage (never client-side), webhook signature verification, HTTPS enforcement, IP allowlisting, token rotation schedule, incident response for key compromise."),
        ("API-M09", "SLA and terms of service for API", "Write the API SLA and ToS: uptime commitment (99.9%), response time targets, rate limits per plan, data retention policies, acceptable use policy, liability limitations, Australian Consumer Law compliance."),
        ("API-M10", "Migration guide template — v1 to v2", "Write a future-proof v1-to-v2 migration guide template. Include: what's changing and why, breaking changes list, deprecation timeline, code examples (before/after), automated migration tool description, support during migration."),
    ]

    # ============================================================
    # AREA 8: Error Message UX Copy (20 agents)
    # ============================================================
    area8_kimi = [
        ("ERR-K01", "Error message management system", "Build a centralized error message management system: error code registry, localization support, severity levels (info/warning/error/critical), contextual suggestions, error code lookup API. Store in JSON with Python accessor module."),
        ("ERR-K02", "Widget-side error display component", "Build a vanilla JS error display component for the widget: toast notifications for minor errors, modal dialogs for critical errors, auto-dismiss timing, retry buttons, 'contact support' links, animations. Shadow DOM encapsulated."),
        ("ERR-K03", "Error recovery flow engine", "Build an error recovery engine: define recovery flows per error type (e.g., mic permission denied -> show instructions -> retry -> escalate to support), track recovery success rate, A/B test recovery flows, timeout to manual recovery."),
        ("ERR-K04", "Error logging and categorization pipeline", "Build an error pipeline: capture client-side errors (widget JS), categorize by type (network/permission/audio/config/server), aggregate for dashboards, alert on error rate spikes, include device/browser context for debugging."),
        ("ERR-K05", "Graceful degradation handler", "Build a graceful degradation system: if voice fails -> offer text chat fallback, if WebRTC unsupported -> offer phone callback, if JS disabled -> show phone number, if widget fails to load -> show minimal contact info. Progressive enhancement."),
        ("ERR-K06", "User-facing error code system", "Build a user-facing error code system (ERR-001 through ERR-099) that maps internal errors to friendly codes. Users can quote these codes to support. Include: code->description lookup, support article URL per code, severity indicator."),
        ("ERR-K07", "Error boundary for widget initialization", "Build error boundaries for widget initialization: catch and handle missing config, invalid API key, blocked domains, CSP violations, script load failures, DOM mounting failures. Each with specific recovery action and user message."),
        ("ERR-K08", "Network error retry logic with user feedback", "Build network error handling with progressive user feedback: first retry silent, second retry show 'reconnecting...', third retry show 'having trouble, please wait', final failure show 'unable to connect' with alternatives. Include offline detection."),
        ("ERR-K09", "Error message A/B testing framework", "Build a framework to A/B test error messages: serve different error copy variants, track which messages lead to successful recovery vs support tickets vs abandonment. Statistical significance calculator for error message optimization."),
        ("ERR-K10", "Error monitoring dashboard queries", "Write PostgreSQL queries and Python functions for an error monitoring dashboard: error rate by type (last 24h/7d/30d), top 10 errors by frequency, error rate by browser/device, recovery rate, support ticket correlation, trend analysis."),
    ]

    area8_minimax = [
        ("ERR-M01", "Complete error message copy — 30 user-friendly messages", "Write 30 user-friendly error messages covering: connection errors, microphone permission, browser compatibility, session timeout, server errors, billing issues, configuration errors. Each: title, description, suggested action. Friendly, not technical."),
        ("ERR-M02", "Microphone permission error flow copy", "Write all copy for the microphone permission error flow: initial request prompt, denied state, instructions per browser (Chrome, Safari, Firefox, Edge), 'still not working' escalation, alternative contact options."),
        ("ERR-M03", "Network and connection error messages", "Write 10 network error messages: slow connection, connection lost, reconnecting, failed to connect, server maintenance, timeout, DNS error, CDN error, WebSocket disconnect, partial load. Each with personality and actionable guidance."),
        ("ERR-M04", "Browser compatibility messages", "Write compatibility messages for: outdated browser, unsupported browser (IE11), WebRTC not supported, cookies disabled, JavaScript disabled, private/incognito mode limitations. Each should redirect to a supported alternative."),
        ("ERR-M05", "Billing and account error messages", "Write 10 billing error messages: payment failed, card expired, subscription paused, account suspended, plan limit reached, trial expired, upgrade required, billing info needed, refund processed, payment method updated."),
        ("ERR-M06", "Widget configuration error messages", "Write 10 configuration error messages: invalid API key, wrong domain, widget ID not found, CORS error (friendly version), embed code outdated, duplicate widget detected, conflicting scripts, missing required fields."),
        ("ERR-M07", "Voice call error messages", "Write 10 voice call error messages: call failed to connect, call dropped, poor audio quality detected, echo detected, caller hung up, maximum call duration reached, no agent available, queue position update, callback scheduled confirmation."),
        ("ERR-M08", "Empty state and zero-data messages", "Write 15 empty state messages: no calls yet, no leads captured, no analytics data, no messages, no team members, no integrations configured, no billing history, no reviews. Each should motivate action, not just state emptiness."),
        ("ERR-M09", "Success and confirmation messages", "Write 15 success/confirmation messages: widget installed, call completed, lead captured, settings saved, payment processed, integration connected, export complete, email sent, schedule updated, review submitted. Celebratory but professional."),
        ("ERR-M10", "Error message style guide", "Write an error message style guide for Sunaiva: voice and tone for errors (empathetic, never blame the user), word choices to avoid ('invalid', 'illegal', 'abort'), formatting rules, emoji usage, length limits, accessibility requirements."),
    ]

    # ============================================================
    # AREA 9: Widget Customization Options Documentation (20 agents)
    # ============================================================
    area9_kimi = [
        ("WCO-K01", "Widget configuration schema with validation", "Build a comprehensive JSON schema for widget configuration: colors (primary, secondary, accent, text, background), position (bottom-right, bottom-left, bottom-center), size (compact, default, large), greeting text, business hours, voice settings. Include Ajv validation."),
        ("WCO-K02", "Live widget customization preview component", "Build a live preview component: real-time widget preview that updates as configuration changes, color picker integration, position toggle, size slider, preview in desktop and mobile views. Pure vanilla JS, no dependencies."),
        ("WCO-K03", "Theme preset system", "Build a theme preset system: 10 pre-built themes (trades: plumber blue, electrician yellow, builder orange, etc.), custom theme builder, theme export/import (JSON), theme validation, apply theme via API. Include CSS custom property mapping."),
        ("WCO-K04", "Widget CSS custom property system", "Build the CSS custom property (variable) system for the widget: define all customizable properties (--sw-primary, --sw-font-family, --sw-border-radius, etc.), default values, fallback chain, documentation of every property."),
        ("WCO-K05", "Widget position and animation configurator", "Build position and animation configuration: 8 position options, entry animations (slide-up, fade-in, bounce, scale), exit animations, hover effects, attention-grabbing pulse, configurable delays and durations. Performance-optimized CSS."),
        ("WCO-K06", "White-label mode for agency partners", "Build white-label configuration: remove Sunaiva branding, custom logo upload (validate dimensions/format), custom brand colors, custom domain for widget assets, custom support email/phone in error messages."),
        ("WCO-K07", "Widget configuration API endpoints", "Build REST API endpoints for widget configuration: GET/PUT /widgets/{id}/config, GET /widgets/{id}/preview, POST /widgets/{id}/themes, GET /themes (presets). Include validation, default values, and config versioning."),
        ("WCO-K08", "Configuration migration and versioning", "Build config versioning: schema version tracking, automatic migration between versions (v1->v2->v3), backward compatibility, migration dry-run mode, rollback capability, config diff visualization."),
        ("WCO-K09", "Widget configuration unit and integration tests", "Write comprehensive tests: config validation (valid configs, boundary values, invalid configs), theme application, position calculation across viewports, animation timing, white-label mode, API endpoints, migration scripts."),
        ("WCO-K10", "Widget embed code generator", "Build an embed code generator: given widget ID and optional config overrides, generate the minimal embed code (script tag). Support: async loading, SRI hash, nonce for CSP, configuration attributes, multiple widget instances per page."),
    ]

    area9_minimax = [
        ("WCO-M01", "Widget customization documentation — complete guide", "Write comprehensive widget customization documentation: every configurable option, default values, examples, screenshots of different configurations, common customization recipes, troubleshooting customization issues."),
        ("WCO-M02", "Trade-specific customization guides — 5 trades", "Write customization guides for 5 trades: plumber (blue theme, emergency button), electrician (yellow theme, safety notice), builder (orange theme, project inquiry form), landscaper (green theme, seasonal options), HVAC (cool blue, maintenance booking)."),
        ("WCO-M03", "Color theory guide for widget branding", "Write a color theory guide for widget branding: how to choose colors that match their business, contrast requirements for accessibility, how to extract brand colors from their existing logo/website, common tradie industry colors."),
        ("WCO-M04", "Widget placement best practices guide", "Write a placement best practices guide: where to position the widget for maximum engagement, mobile vs desktop considerations, avoiding conflict with other page elements, A/B testing placement, heat map insights for widget placement."),
        ("WCO-M05", "Greeting message templates — 20 templates", "Write 20 greeting message templates for different trades and scenarios: general business hours, after-hours, emergency plumber, appointment-based electrician, quote-request builder, seasonal HVAC, weekend availability, holiday message."),
        ("WCO-M06", "White-label setup guide for agencies", "Write the white-label setup guide for agency partners: how to remove Sunaiva branding, upload custom logo, set brand colors, configure custom domain, manage multiple client configurations, brand consistency tips."),
        ("WCO-M07", "Widget customization FAQ — 15 questions", "Write 15 FAQ entries about widget customization: Can I match my website colors? Can I add my logo? Can I change the widget position? Can I customize what the AI says? Can I have different settings for different pages? How do I preview changes?"),
        ("WCO-M08", "Advanced customization recipes cookbook", "Write 10 advanced customization recipes: 1) Match widget to WordPress theme, 2) Holiday-themed widget, 3) After-hours mode with emergency redirect, 4) Multi-language greeting, 5) Industry-specific lead form, 6-10) more creative customizations."),
        ("WCO-M09", "Customization limitations and workarounds", "Write a transparent document about customization limitations: what can't be customized (voice engine internals, call routing logic), workarounds for common requests, feature request process, planned customization features on the roadmap."),
        ("WCO-M10", "Visual style guide for the widget", "Write a visual style guide: Sunaiva widget design principles, default theme documentation, typography choices, icon set, spacing system, shadow and elevation, border radius philosophy, animation principles, dark mode guidelines."),
    ]

    # ============================================================
    # AREA 10: Competitor Comparison Content (20 agents)
    # ============================================================
    area10_kimi = [
        ("CMP-K01", "Competitor data scraping and analysis tool", "Build a Python tool that scrapes competitor pricing and features from: Intercom, Drift, LiveChat, Tidio, Crisp, Smith.ai, Ruby Receptionists, Podium. Extract: pricing tiers, features per tier, customer reviews. Output structured JSON."),
        ("CMP-K02", "Feature comparison matrix generator", "Build a comparison matrix generator: input features and competitors, output interactive HTML comparison table. Support: feature categories, tooltips, checkmarks/x-marks, 'most popular' highlighting, responsive design, shareable URL."),
        ("CMP-K03", "TCO (Total Cost of Ownership) calculator", "Build a TCO calculator: input business size, call volume, hours of operation. Calculate total cost for: Sunaiva vs hiring receptionist vs answering service vs chatbot vs doing nothing. Include hidden costs (training, turnover, missed calls)."),
        ("CMP-K04", "ROI calculator widget", "Build an interactive ROI calculator widget: inputs (current missed calls/month, average job value, current receptionist cost), outputs (projected leads captured, revenue increase, cost savings, payback period). Embeddable, shareable results."),
        ("CMP-K05", "Competitive intelligence monitoring system", "Build a monitoring system that tracks competitor changes: pricing updates, new features, blog posts, customer reviews, social media mentions. Daily digest email. Store historical data in PostgreSQL for trend analysis."),
        ("CMP-K06", "Comparison page SEO optimization module", "Build a module that generates SEO-optimized comparison pages: 'Sunaiva vs [Competitor]' pages with proper schema markup (Product comparison), meta tags, internal linking, FAQ schema. Template-driven for easy scaling."),
        ("CMP-K07", "Switching cost calculator", "Build a switching cost calculator for businesses considering moving from a competitor to Sunaiva: import existing data, onboarding time, feature parity check, migration assistance value, break-even analysis. Output migration plan."),
        ("CMP-K08", "Customer review sentiment analyzer", "Build a sentiment analyzer for competitor reviews: scrape G2/Capterra/TrustRadius reviews, analyze sentiment by feature category, identify competitor weaknesses, generate content ideas from negative reviews. Use TF-IDF + rule-based sentiment."),
        ("CMP-K09", "Competitive battlecard generator", "Build a battlecard generator: input competitor name, output a 1-page sales battlecard with: competitor overview, their strengths, their weaknesses, Sunaiva advantages, common objections + rebuttals, closing arguments. PDF output."),
        ("CMP-K10", "Win/loss analysis tracking system", "Build a win/loss tracking system: log competitive deals (won/lost/pending), record why customer chose us or competitor, aggregate insights (top reasons for wins/losses), generate quarterly competitive intelligence reports."),
    ]

    area10_minimax = [
        ("CMP-M01", "Sunaiva vs Generic Chatbots — comparison page", "Write a full comparison page: Sunaiva AI Voice vs generic chatbots (Tidio, Crisp, Tawk.to). Cover: voice vs text engagement rates, AI sophistication, setup complexity, pricing, customer experience. Conclude with clear winner for tradies."),
        ("CMP-M02", "Sunaiva vs Live Chat Services — comparison page", "Write a full comparison page: Sunaiva vs live chat services (LiveChat, Intercom, Drift). Cover: 24/7 availability without staff, cost per interaction, handling complex trade questions, scalability, Australian support."),
        ("CMP-M03", "Sunaiva vs Virtual Receptionist Services — comparison", "Write comparison: Sunaiva vs virtual receptionist services (Ruby, Smith.ai, AnswerConnect). Cover: cost per call, availability, trade knowledge, scalability, setup time, customization, consistency of service."),
        ("CMP-M04", "Sunaiva vs Hiring a Receptionist — cost comparison", "Write detailed cost comparison: Sunaiva ($197-597/mo) vs hiring a receptionist ($50-70K/yr salary + super + leave + training + desk + phone). Include: after-hours coverage, sick days, scalability, consistency. Australian figures."),
        ("CMP-M05", "Sunaiva vs DIY Phone System — comparison", "Write comparison: Sunaiva vs DIY solutions (Google Voice, RingCentral, Grasshopper). Cover: AI intelligence vs dumb routing, lead capture, appointment booking, cost, setup complexity, missed call rates."),
        ("CMP-M06", "10 reasons to choose Sunaiva over alternatives", "Write a compelling '10 reasons' article: 1) Voice vs text, 2) 24/7 without staff, 3) Tradie-trained AI, 4) Australian-first, 5) No contracts, 6) 3-minute setup, 7) Real lead capture, 8) Fraction of receptionist cost, 9) Scales instantly, 10) Agency partner program."),
        ("CMP-M07", "Competitive objection handling scripts — 15 objections", "Write 15 objection handling scripts for sales conversations: 'It's too expensive', 'We already have a chatbot', 'Our customers prefer talking to humans', 'We don't get enough calls', 'AI can't handle our industry', etc. 3-step rebuttals each."),
        ("CMP-M08", "Switching from competitor guide — 3 guides", "Write 3 switching guides: 1) Switching from Intercom/Drift (text to voice), 2) Switching from answering service (human to AI), 3) Switching from no solution (missed calls to captured leads). Include data migration and setup steps."),
        ("CMP-M09", "Market positioning statement and elevator pitches", "Write: 1) Official positioning statement (50 words), 2) Three 30-second elevator pitches (for tradies, for agencies, for investors), 3) Tagline options (10), 4) One-liner descriptions (5). All differentiated from competitors."),
        ("CMP-M10", "Competitive landscape summary for investors/partners", "Write a competitive landscape summary suitable for investor decks or partner pitches: market size (Australian AI voice market), key competitors and their positioning, Sunaiva's unique advantages, moat analysis, growth opportunity."),
    ]

    # ============================================================
    # ASSEMBLE ALL STORIES
    # ============================================================
    all_areas = [
        (area1_kimi, area1_minimax, "Widget.js Telnyx Obfuscation"),
        (area2_kimi, area2_minimax, "Mobile-Responsive Testing"),
        (area3_kimi, area3_minimax, "SEO Meta Tags & Open Graph"),
        (area4_kimi, area4_minimax, "Customer Onboarding Emails"),
        (area5_kimi, area5_minimax, "FAQ Content for Widget"),
        (area6_kimi, area6_minimax, "Testimonial & Social Proof"),
        (area7_kimi, area7_minimax, "API Documentation Partners"),
        (area8_kimi, area8_minimax, "Error Message UX Copy"),
        (area9_kimi, area9_minimax, "Widget Customization Docs"),
        (area10_kimi, area10_minimax, "Competitor Comparison Content"),
    ]

    for kimi_stories, minimax_stories, area_name in all_areas:
        for sid, title, prompt in kimi_stories:
            stories.append({
                "id": sid,
                "title": title,
                "prompt": prompt,
                "model": "kimi",
                "area": area_name,
                "agent_id": story_id
            })
            story_id += 1

        for sid, title, prompt in minimax_stories:
            stories.append({
                "id": sid,
                "title": title,
                "prompt": prompt,
                "model": "minimax",
                "area": area_name,
                "agent_id": story_id
            })
            story_id += 1

    return stories


@dataclass
class AgentResult:
    agent_id: int
    story_id: str
    title: str
    area: str
    model: str
    model_id: str
    status: str
    prompt_tokens: int
    completion_tokens: int
    total_tokens: int
    cost_usd: float
    response_time_ms: int
    response: Optional[str] = None
    error: Optional[str] = None
    timestamp: str = None

    def __post_init__(self):
        if self.timestamp is None:
            self.timestamp = datetime.now().isoformat()


async def execute_agent(
    session: aiohttp.ClientSession,
    story: dict,
    api_key: str
) -> AgentResult:
    """Execute a single agent task via OpenRouter API."""
    start_time = time.time()

    model_id = KIMI_MODEL_ID if story["model"] == "kimi" else MINIMAX_MODEL_ID
    system_prompt = KIMI_SYSTEM_PROMPT if story["model"] == "kimi" else MINIMAX_SYSTEM_PROMPT

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
        "HTTP-Referer": "https://github.com/genesis-system",
        "X-Title": "Genesis Session 22 Swarm"
    }

    payload = {
        "model": model_id,
        "messages": [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": f"# Task: {story['title']}\n\n{story['prompt']}\n\nProvide a complete, production-ready deliverable. Be thorough."}
        ],
        "temperature": 0.7,
        "max_tokens": 8192
    }

    try:
        timeout = aiohttp.ClientTimeout(total=REQUEST_TIMEOUT_SECONDS)
        async with session.post(OPENROUTER_BASE_URL, json=payload, headers=headers, timeout=timeout) as response:
            response_time_ms = int((time.time() - start_time) * 1000)

            if response.status != 200:
                error_text = await response.text()
                return AgentResult(
                    agent_id=story["agent_id"],
                    story_id=story["id"],
                    title=story["title"],
                    area=story["area"],
                    model=story["model"],
                    model_id=model_id,
                    status="fail",
                    prompt_tokens=0,
                    completion_tokens=0,
                    total_tokens=0,
                    cost_usd=0.0,
                    response_time_ms=response_time_ms,
                    error=f"HTTP {response.status}: {error_text[:500]}"
                )

            data = await response.json()

            # Check for API-level errors
            if "error" in data:
                return AgentResult(
                    agent_id=story["agent_id"],
                    story_id=story["id"],
                    title=story["title"],
                    area=story["area"],
                    model=story["model"],
                    model_id=model_id,
                    status="fail",
                    prompt_tokens=0,
                    completion_tokens=0,
                    total_tokens=0,
                    cost_usd=0.0,
                    response_time_ms=response_time_ms,
                    error=f"API Error: {json.dumps(data['error'])[:500]}"
                )

            usage = data.get("usage", {})
            prompt_tokens = usage.get("prompt_tokens", 0)
            completion_tokens = usage.get("completion_tokens", 0)
            total_tokens = usage.get("total_tokens", 0)

            # Cost calculation
            cost_per_mtok = 1.07 if story["model"] == "kimi" else 1.00
            cost_usd = (total_tokens / 1_000_000) * cost_per_mtok

            response_text = data["choices"][0]["message"]["content"]

            return AgentResult(
                agent_id=story["agent_id"],
                story_id=story["id"],
                title=story["title"],
                area=story["area"],
                model=story["model"],
                model_id=model_id,
                status="success",
                prompt_tokens=prompt_tokens,
                completion_tokens=completion_tokens,
                total_tokens=total_tokens,
                cost_usd=cost_usd,
                response_time_ms=response_time_ms,
                response=response_text
            )

    except asyncio.TimeoutError:
        return AgentResult(
            agent_id=story["agent_id"],
            story_id=story["id"],
            title=story["title"],
            area=story["area"],
            model=story["model"],
            model_id=model_id,
            status="fail",
            prompt_tokens=0,
            completion_tokens=0,
            total_tokens=0,
            cost_usd=0.0,
            response_time_ms=int((time.time() - start_time) * 1000),
            error=f"Timeout after {REQUEST_TIMEOUT_SECONDS}s"
        )
    except Exception as e:
        return AgentResult(
            agent_id=story["agent_id"],
            story_id=story["id"],
            title=story["title"],
            area=story["area"],
            model=story["model"],
            model_id=model_id,
            status="fail",
            prompt_tokens=0,
            completion_tokens=0,
            total_tokens=0,
            cost_usd=0.0,
            response_time_ms=int((time.time() - start_time) * 1000),
            error=str(e)[:500]
        )


async def execute_batch(
    session: aiohttp.ClientSession,
    stories: list,
    api_key: str,
    batch_size: int,
    team_name: str
) -> list:
    """Execute stories in batches."""
    results = []
    total = len(stories)

    for i in range(0, total, batch_size):
        batch = stories[i:i + batch_size]
        batch_num = (i // batch_size) + 1
        total_batches = (total + batch_size - 1) // batch_size

        print(f"  [{team_name}] Batch {batch_num}/{total_batches}: {len(batch)} agents...", flush=True)

        try:
            batch_results = await asyncio.wait_for(
                asyncio.gather(
                    *[execute_agent(session, s, api_key) for s in batch]
                ),
                timeout=BATCH_TIMEOUT_SECONDS
            )
            results.extend(batch_results)

            successes = len([r for r in batch_results if r.status == "success"])
            failures = len([r for r in batch_results if r.status == "fail"])
            batch_cost = sum(r.cost_usd for r in batch_results)
            print(f"    OK: {successes} success, {failures} failed, ${batch_cost:.4f}", flush=True)

            # Rate limit spacing for Kimi
            if "kimi" in team_name.lower() and i + batch_size < total:
                print(f"    Waiting 10s (Kimi rate limit)...", flush=True)
                await asyncio.sleep(10)

        except asyncio.TimeoutError:
            print(f"    TIMEOUT: Batch {batch_num} timed out after {BATCH_TIMEOUT_SECONDS}s", flush=True)
            for s in batch:
                results.append(AgentResult(
                    agent_id=s["agent_id"],
                    story_id=s["id"],
                    title=s["title"],
                    area=s["area"],
                    model=s["model"],
                    model_id=KIMI_MODEL_ID if s["model"] == "kimi" else MINIMAX_MODEL_ID,
                    status="fail",
                    prompt_tokens=0,
                    completion_tokens=0,
                    total_tokens=0,
                    cost_usd=0.0,
                    response_time_ms=BATCH_TIMEOUT_SECONDS * 1000,
                    error=f"Batch timeout after {BATCH_TIMEOUT_SECONDS}s"
                ))

    return results


def save_results(results: list, output_dir: str):
    """Save results to individual files and summary JSONL."""
    os.makedirs(output_dir, exist_ok=True)
    os.makedirs(f"{output_dir}/kimi", exist_ok=True)
    os.makedirs(f"{output_dir}/minimax", exist_ok=True)

    # Save individual response files
    for r in results:
        if r.status == "success" and r.response:
            subdir = "kimi" if r.model == "kimi" else "minimax"
            filepath = f"{output_dir}/{subdir}/{r.story_id}.md"
            with open(filepath, "w", encoding="utf-8") as f:
                f.write(f"# {r.story_id}: {r.title}\n")
                f.write(f"**Area:** {r.area}\n")
                f.write(f"**Model:** {r.model_id}\n")
                f.write(f"**Tokens:** {r.total_tokens:,}\n")
                f.write(f"**Cost:** ${r.cost_usd:.4f}\n")
                f.write(f"**Time:** {r.response_time_ms}ms\n")
                f.write(f"**Timestamp:** {r.timestamp}\n\n---\n\n")
                f.write(r.response)

    # Save JSONL summaries
    kimi_results = [r for r in results if r.model == "kimi"]
    minimax_results = [r for r in results if r.model == "minimax"]

    with open(f"{output_dir}/kimi_results.jsonl", "w") as f:
        for r in kimi_results:
            d = asdict(r)
            d.pop("response", None)  # Don't duplicate in JSONL
            f.write(json.dumps(d) + "\n")

    with open(f"{output_dir}/minimax_results.jsonl", "w") as f:
        for r in minimax_results:
            d = asdict(r)
            d.pop("response", None)
            f.write(json.dumps(d) + "\n")

    return kimi_results, minimax_results


def generate_report(kimi_results, minimax_results, output_dir):
    """Generate execution report."""
    all_results = kimi_results + minimax_results
    total = len(all_results)
    successes = len([r for r in all_results if r.status == "success"])
    failures = len([r for r in all_results if r.status == "fail"])
    total_tokens = sum(r.total_tokens for r in all_results)
    total_cost = sum(r.cost_usd for r in all_results)
    avg_time = sum(r.response_time_ms for r in all_results) / total if total > 0 else 0

    # Area breakdown
    areas = {}
    for r in all_results:
        if r.area not in areas:
            areas[r.area] = {"total": 0, "success": 0, "fail": 0, "cost": 0.0, "tokens": 0}
        areas[r.area]["total"] += 1
        areas[r.area]["cost"] += r.cost_usd
        areas[r.area]["tokens"] += r.total_tokens
        if r.status == "success":
            areas[r.area]["success"] += 1
        else:
            areas[r.area]["fail"] += 1

    report = f"""# Session 22 Swarm Execution Report
**Sunaiva Talking Widget — 200 Agent Swarm**

**Generated:** {datetime.now().isoformat()}

## Overview

| Metric | Value |
|--------|-------|
| **Total Agents Fired** | {total} |
| **Kimi K2.5 Agents** | {len(kimi_results)} |
| **MiniMax M2.5 Agents** | {len(minimax_results)} |
| **Successful** | {successes} ({successes/total*100:.1f}%) |
| **Failed** | {failures} |
| **Total Tokens** | {total_tokens:,} |
| **Total Cost** | ${total_cost:.4f} |
| **Avg Response Time** | {avg_time:.0f}ms |

## Team Breakdown

### Kimi K2.5 (Technical/Code)
| Metric | Value |
|--------|-------|
| Agents | {len(kimi_results)} |
| Success | {len([r for r in kimi_results if r.status == 'success'])} |
| Failed | {len([r for r in kimi_results if r.status == 'fail'])} |
| Tokens | {sum(r.total_tokens for r in kimi_results):,} |
| Cost | ${sum(r.cost_usd for r in kimi_results):.4f} |

### MiniMax M2.5 (Content/Marketing)
| Metric | Value |
|--------|-------|
| Agents | {len(minimax_results)} |
| Success | {len([r for r in minimax_results if r.status == 'success'])} |
| Failed | {len([r for r in minimax_results if r.status == 'fail'])} |
| Tokens | {sum(r.total_tokens for r in minimax_results):,} |
| Cost | ${sum(r.cost_usd for r in minimax_results):.4f} |

## Area Breakdown

| Area | Total | Success | Failed | Tokens | Cost |
|------|-------|---------|--------|--------|------|
"""

    for area_name, m in sorted(areas.items()):
        report += f"| {area_name} | {m['total']} | {m['success']} | {m['fail']} | {m['tokens']:,} | ${m['cost']:.4f} |\n"

    # Failed stories
    failed = [r for r in all_results if r.status == "fail"]
    if failed:
        report += f"\n## Failed Stories ({len(failed)})\n\n"
        for r in failed:
            report += f"- **{r.story_id}** ({r.model}): {r.error}\n"
    else:
        report += "\n## No Failures!\n\nAll 200 agents executed successfully.\n"

    report += f"""
## Output Files

- Individual responses: `{output_dir}/kimi/` and `{output_dir}/minimax/`
- Kimi summary: `{output_dir}/kimi_results.jsonl`
- MiniMax summary: `{output_dir}/minimax_results.jsonl`
- This report: `{output_dir}/SWARM_REPORT.md`

---
**Generated by:** Genesis Session 22 Swarm Launcher
**Timestamp:** {datetime.now().isoformat()}
"""

    with open(f"{output_dir}/SWARM_REPORT.md", "w") as f:
        f.write(report)

    return report


async def main():
    print(f"\n{'='*80}")
    print("GENESIS SESSION 22 SWARM — 200 AGENTS")
    print(f"{'='*80}")
    print(f"Timestamp: {datetime.now().isoformat()}")
    print(f"Models: Kimi K2.5 (100 agents) + MiniMax M2.5 (100 agents)")
    print(f"Target: Sunaiva Talking Widget — 10 work areas")
    print(f"Output: {OUTPUT_DIR}")
    print(f"{'='*80}\n")

    # Generate stories
    print("Generating 200 stories...", flush=True)
    all_stories = generate_stories()

    kimi_stories = [s for s in all_stories if s["model"] == "kimi"]
    minimax_stories = [s for s in all_stories if s["model"] == "minimax"]

    print(f"  Total: {len(all_stories)} stories")
    print(f"  Kimi: {len(kimi_stories)} stories (technical/code)")
    print(f"  MiniMax: {len(minimax_stories)} stories (content/marketing)")
    print()

    api_key = OPENROUTER_API_KEY
    print("API key loaded.", flush=True)

    all_results = []

    async with aiohttp.ClientSession() as session:
        # Fire MiniMax first (larger batches, faster)
        print(f"\n{'='*80}")
        print(f"TEAM MINIMAX: {len(minimax_stories)} agents (batch size {BATCH_SIZE_MINIMAX})")
        print(f"{'='*80}", flush=True)

        minimax_results = await execute_batch(
            session, minimax_stories, api_key, BATCH_SIZE_MINIMAX, "MINIMAX"
        )
        all_results.extend(minimax_results)

        # Then Kimi (smaller batches, rate limited)
        print(f"\n{'='*80}")
        print(f"TEAM KIMI: {len(kimi_stories)} agents (batch size {BATCH_SIZE_KIMI})")
        print(f"{'='*80}", flush=True)

        kimi_results = await execute_batch(
            session, kimi_stories, api_key, BATCH_SIZE_KIMI, "KIMI"
        )
        all_results.extend(kimi_results)

    # Save results
    print(f"\n{'='*80}")
    print("SAVING RESULTS")
    print(f"{'='*80}", flush=True)

    kimi_r, minimax_r = save_results(all_results, OUTPUT_DIR)

    # Generate report
    report = generate_report(
        [r for r in all_results if r.model == "kimi"],
        [r for r in all_results if r.model == "minimax"],
        OUTPUT_DIR
    )

    # Final summary
    total = len(all_results)
    successes = len([r for r in all_results if r.status == "success"])
    failures = len([r for r in all_results if r.status == "fail"])
    total_cost = sum(r.cost_usd for r in all_results)
    total_tokens = sum(r.total_tokens for r in all_results)

    print(f"\n{'='*80}")
    print("SWARM EXECUTION COMPLETE")
    print(f"{'='*80}")
    print(f"  Total Agents: {total}")
    print(f"  Successes: {successes} ({successes/total*100:.1f}%)")
    print(f"  Failures: {failures}")
    print(f"  Total Tokens: {total_tokens:,}")
    print(f"  Total Cost: ${total_cost:.4f}")
    print(f"  Report: {OUTPUT_DIR}/SWARM_REPORT.md")
    print(f"{'='*80}\n")


if __name__ == "__main__":
    asyncio.run(main())
