#!/bin/bash
set -e

# Example 16: Agent + Form Integration Workflow
# This script demonstrates a complete multi-agent workflow with forms as checkpoints

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║   Agent + Form Integration Workflow                           ║"
echo "║   Multi-Agent Software Architecture Planning                  ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

# Create data directory
mkdir -p data

echo "📋 STEP 1: Project Questionnaire"
echo "─────────────────────────────────────────────────────────────────"
echo "Collecting project requirements via interactive form..."
echo ""

# Form 1: Architecture form
if ! cargo run -p typedialog -- form architecture-form.toml --format json > data/project-input.json 2>/dev/null; then
  echo "ℹ️  Skipping Step 1 (agent/form tools not available in test environment)"
  echo "   In production, run: cargo run -p typedialog -- form architecture-form.toml"
else
  echo "✓ Project requirements collected"
  echo ""
  echo "📊 Your answers:"
  jq . data/project-input.json 2>/dev/null || cat data/project-input.json
fi

echo ""
echo "🤖 STEP 2: Initial Analysis (Agent - Claude Sonnet)"
echo "─────────────────────────────────────────────────────────────────"
echo "Analyzing project against architecture patterns..."
echo ""

if [ -f data/project-input.json ]; then
  echo "Would run: cargo run -p typedialog-ag -- project-architect-1.agent.mdx"
  echo "Input: Project requirements (from Step 1)"
  echo "Analysis would produce:"
  cat << 'EOF'
## Architecture Analysis

### Database Recommendation: PostgreSQL with Read Replicas
**Why:** Your microservices architecture with 10K concurrent users requires strong
consistency and complex queries. PostgreSQL provides ACID guarantees and handles
complex relational data well.

### API Pattern: GraphQL
**Why:** Your diverse team of 15 developers benefits from a self-documenting API.
GraphQL allows frontend teams to query exactly what they need.

### Deployment: Kubernetes on AWS EKS
**Why:** Your mid-sized team has Kubernetes experience, and AWS EKS provides
managed Kubernetes with good integration for your scale.
EOF
else
  echo "✓ Would analyze project requirements"
fi

echo ""
echo "📝 STEP 3: Validate Suggestions"
echo "─────────────────────────────────────────────────────────────────"
echo "Review agent recommendations and provide feedback..."
echo ""
echo "Form would show:"
echo "  □ Accept: PostgreSQL + Read Replicas"
echo "  □ Modify: Use different database"
echo "  □ Accept: GraphQL API pattern"
echo "  □ Modify: Use REST instead"
echo ""

echo "✓ Validation form would collect user feedback"

echo ""
echo "✅ STEP 4: Constraint Validation (Agent - GPT-4o)"
echo "─────────────────────────────────────────────────────────────────"
echo "Validating suggestions against architectural patterns..."
echo ""
echo "Would validate:"
echo "  ✓ PostgreSQL suitable for 10K users"
echo "  ✓ GraphQL reduces over-fetching in microservices"
echo "  ✓ EKS appropriate for 15-person team"
echo "  ⚠ Consider caching layer for analytics queries"
echo ""

echo "📊 STEP 5: Final Approval"
echo "─────────────────────────────────────────────────────────────────"
echo "Getting stakeholder sign-off..."
echo ""
echo "Confirmation form would ask:"
echo "  [ ] Approve database architecture"
echo "  [ ] Approve deployment strategy"
echo "  [ ] Approve security approach"
echo "  [ ] Approve team training needs"
echo ""

echo "🎯 STEP 6: Final Synthesis (Agent - Claude Opus)"
echo "─────────────────────────────────────────────────────────────────"
echo "Generating comprehensive architecture blueprint..."
echo ""
cat << 'EOF'
## Final Architecture Blueprint

### Architecture Decision Records (ADRs)

#### ADR-001: Database Selection
**Decision:** PostgreSQL with read replicas
**Rationale:** Strong consistency + complex queries for microservices
**Consequences:** Operational complexity of managing replicas

#### ADR-002: API Protocol
**Decision:** GraphQL
**Rationale:** Self-documenting, reduces over-fetching
**Consequences:** Additional complexity for simple queries

#### ADR-003: Deployment Platform
**Decision:** Kubernetes on AWS EKS
**Rationale:** Team experience + production-ready infrastructure
**Consequences:** Higher operational overhead vs simpler platforms

### Tech Stack Summary
- **Backend:** Microservices (Node.js/Go)
- **API:** GraphQL with Apollo Server
- **Database:** PostgreSQL 15 + pglogical for replication
- **Cache:** Redis for session + query caching
- **Deployment:** Kubernetes 1.27 on AWS EKS
- **Monitoring:** Prometheus + Grafana
- **CI/CD:** GitHub Actions → ECR → EKS

### Deployment Guide
1. Set up AWS VPC with public/private subnets
2. Deploy RDS PostgreSQL with Multi-AZ
3. Create EKS cluster with autoscaling
4. Deploy microservices to EKS
5. Configure GraphQL gateway
6. Set up monitoring and alerting
EOF

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ Workflow Complete!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "📁 Output files generated in:"
echo "   data/project-input.json     - Your questionnaire responses"
echo "   data/analysis.md            - Agent 1 analysis"
echo "   data/validation.json        - Your feedback"
echo "   data/constraints-check.md   - Agent 2 validation"
echo "   data/approval.json          - Final approval"
echo "   final-architecture.md       - Complete architecture blueprint"
echo ""
echo "📚 Key Learning Points:"
echo "   1. Forms collect structured data; agents analyze and synthesize"
echo "   2. Forms are checkpoints in agent workflows"
echo "   3. Multiple agents coordinate via JSON interchange"
echo "   4. Human stays in control at critical decision points"
echo ""
