-- GooseFactory: Seed Agents and Decision Tasks
-- Created: 2026-02-07
-- Purpose: Populate factory with AI worker agents and realistic decision tasks

BEGIN;

-- ============================================================================
-- PART 1: Create Factory Agents
-- ============================================================================

INSERT INTO agents (id, name, type, status, health, capabilities, config, tasks_completed_total, last_heartbeat, uptime_seconds, error_count_24h)
VALUES
  -- Builder Agent: Active AI builder, currently working on Meta Ads MCP
  (
    'a1000000-0000-0000-0000-000000000001'::uuid,
    'Builder Agent Alpha',
    'ai_builder',
    'active',
    '{"cpu": 45, "memory": 62, "status": "healthy"}'::jsonb,
    '["typescript", "node", "mcp-sdk", "api-integration", "code-generation"]'::jsonb,
    '{"model": "claude-sonnet-4", "max_concurrent_tasks": 3}'::jsonb,
    127,
    NOW() - INTERVAL '2 minutes',
    86400,
    0
  ),
  
  -- Test Runner Agent: Idle, waiting for test tasks
  (
    'a2000000-0000-0000-0000-000000000002'::uuid,
    'Test Runner Beta',
    'test_runner',
    'idle',
    '{"cpu": 12, "memory": 28, "status": "healthy"}'::jsonb,
    '["jest", "vitest", "integration-testing", "api-testing", "test-analysis"]'::jsonb,
    '{"test_frameworks": ["jest", "vitest"], "parallel_runs": 4}'::jsonb,
    89,
    NOW() - INTERVAL '5 minutes',
    172800,
    0
  ),
  
  -- Deployer Agent: Idle, ready for deployment tasks
  (
    'a3000000-0000-0000-0000-000000000003'::uuid,
    'Deployer Gamma',
    'deployer',
    'idle',
    '{"cpu": 8, "memory": 15, "status": "healthy"}'::jsonb,
    '["docker", "npm-publish", "github-actions", "vercel", "deployment-automation"]'::jsonb,
    '{"registries": ["npm", "github"], "auto_deploy": false}'::jsonb,
    52,
    NOW() - INTERVAL '1 minute',
    259200,
    1
  ),
  
  -- Monitor Agent: Active, continuously monitoring all pipelines
  (
    'a4000000-0000-0000-0000-000000000004'::uuid,
    'Monitor Delta',
    'monitor',
    'active',
    '{"cpu": 18, "memory": 34, "status": "healthy"}'::jsonb,
    '["health-checks", "log-analysis", "performance-monitoring", "alerting", "metrics"]'::jsonb,
    '{"check_interval_seconds": 60, "alert_threshold": "error"}'::jsonb,
    312,
    NOW() - INTERVAL '30 seconds',
    432000,
    0
  ),
  
  -- Code Reviewer Agent: Idle, waiting for review tasks
  (
    'a5000000-0000-0000-0000-000000000005'::uuid,
    'Code Reviewer Epsilon',
    'ai_builder',
    'idle',
    '{"cpu": 22, "memory": 41, "status": "healthy"}'::jsonb,
    '["code-review", "security-audit", "best-practices", "documentation-review", "api-design"]'::jsonb,
    '{"model": "claude-opus-4", "review_depth": "comprehensive"}'::jsonb,
    67,
    NOW() - INTERVAL '3 minutes',
    345600,
    0
  );

-- ============================================================================
-- PART 2: Create Decision Tasks Based on Real Pipeline State
-- ============================================================================

-- Task 1: Fix Required - GHL MCP has 42 failing tests
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  '11000000-0000-0000-0000-000000000001'::uuid,
  p.id,
  'testing',
  'fix_required',
  'Fix 42 Failing Tests in GoHighLevel MCP',
  'The GoHighLevel MCP server has 42 failing integration tests that are blocking progression to review stage. Tests are failing in authentication, webhook handling, and contact management modules.',
  '{"failing_test_count": 42, "affected_modules": ["auth", "webhooks", "contacts"], "test_framework": "jest", "logs_url": "file://logs/ghl-test-failures.log"}'::jsonb,
  'pending',
  'critical',
  true,
  NOW() + INTERVAL '24 hours',
  NOW() - INTERVAL '3 hours'
FROM pipelines p WHERE p.slug = 'gohighlevel-mcp'
LIMIT 1;

-- Task 2: Review - Close MCP at review stage
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  '22000000-0000-0000-0000-000000000002'::uuid,
  p.id,
  'review',
  'review',
  'Code Review: Close CRM MCP Server',
  'Review the Close CRM MCP implementation for code quality, security best practices, API coverage, and documentation completeness before staging deployment.',
  '{"review_checklist": ["code_quality", "security", "api_coverage", "documentation", "error_handling"], "estimated_review_time_hours": 4}'::jsonb,
  'pending',
  'high',
  true,
  NOW() + INTERVAL '48 hours',
  NOW() - INTERVAL '6 hours'
FROM pipelines p WHERE p.slug = 'close'
LIMIT 1;

-- Task 3: Review - FreshDesk MCP at review stage
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  '33000000-0000-0000-0000-000000000003'::uuid,
  p.id,
  'review',
  'review',
  'Code Review: FreshDesk Support MCP',
  'Comprehensive review of FreshDesk MCP server implementation. Focus on ticket management tools, API rate limiting, and webhook reliability.',
  '{"review_areas": ["ticket_tools", "rate_limiting", "webhooks", "authentication"], "priority_concerns": ["security", "reliability"]}'::jsonb,
  'pending',
  'high',
  true,
  NOW() + INTERVAL '48 hours',
  NOW() - INTERVAL '8 hours'
FROM pipelines p WHERE p.slug = 'freshdesk'
LIMIT 1;

-- Task 4: Review - HelpScout MCP at review stage
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  '44000000-0000-0000-0000-000000000004'::uuid,
  p.id,
  'review',
  'review',
  'Code Review: HelpScout Customer Support MCP',
  'Review HelpScout MCP server for production readiness. Verify conversation threading, customer data handling, and search functionality.',
  '{"review_focus": ["conversation_tools", "customer_privacy", "search_performance"], "compliance_check": true}'::jsonb,
  'pending',
  'medium',
  true,
  NOW() + INTERVAL '72 hours',
  NOW() - INTERVAL '12 hours'
FROM pipelines p WHERE p.slug = 'helpscout'
LIMIT 1;

-- Task 5: Manual Action - Meta Ads needs API credentials
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  '55000000-0000-0000-0000-000000000005'::uuid,
  p.id,
  'building',
  'manual_action',
  'Configure Meta Ads API Credentials',
  'Meta Ads MCP requires Facebook Business Manager API keys and OAuth configuration. Need to create a Facebook App, configure OAuth redirect URLs, and generate access tokens for testing.',
  '{"required_credentials": ["app_id", "app_secret", "access_token"], "setup_docs": "https://developers.facebook.com/docs/marketing-api", "business_manager_required": true}'::jsonb,
  'pending',
  'high',
  true,
  NOW() + INTERVAL '24 hours',
  NOW() - INTERVAL '5 hours'
FROM pipelines p WHERE p.slug = 'meta-ads-mcp'
LIMIT 1;

-- Task 6: Manual Action - Twilio needs API credentials
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  '66000000-0000-0000-0000-000000000006'::uuid,
  p.id,
  'building',
  'manual_action',
  'Set Up Twilio API Keys and Test Phone Number',
  'Twilio MCP requires Account SID, Auth Token, and a verified test phone number for SMS/voice testing. Configure environment variables and verify webhook endpoints.',
  '{"required_credentials": ["account_sid", "auth_token", "phone_number"], "webhook_url_needed": true, "test_credits_available": true}'::jsonb,
  'pending',
  'high',
  true,
  NOW() + INTERVAL '24 hours',
  NOW() - INTERVAL '4 hours'
FROM pipelines p WHERE p.slug = 'twilio-mcp'
LIMIT 1;

-- Task 7: Approval - CloseBot needs design review
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  '77000000-0000-0000-0000-000000000007'::uuid,
  p.id,
  'building',
  'approval',
  'Approve CloseBot MCP Architecture Design',
  'CloseBot MCP has a unique conversational AI integration that requires architectural approval. Review the proposed Claude integration pattern, conversation state management, and CRM sync strategy.',
  '{"design_docs": "docs/closebot-architecture.md", "approval_needed_from": "lead_architect", "key_concerns": ["ai_integration", "state_management", "crm_sync"]}'::jsonb,
  'pending',
  'medium',
  true,
  NOW() + INTERVAL '96 hours',
  NOW() - INTERVAL '18 hours'
FROM pipelines p WHERE p.slug = 'closebot-mcp'
LIMIT 1;

-- Task 8: Approval - Google Console needs design approval
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  '88000000-0000-0000-0000-000000000008'::uuid,
  p.id,
  'building',
  'approval',
  'Approve Google Search Console MCP Scope',
  'Google Search Console MCP scope needs approval. Proposed features include SEO metrics, indexing status, and sitemap management. Decide if we should also include Google Analytics integration in v1.',
  '{"proposed_features": ["seo_metrics", "indexing", "sitemaps"], "scope_question": "include_analytics_v1", "estimated_effort_days": 8}'::jsonb,
  'pending',
  'medium',
  false,
  NOW() + INTERVAL '120 hours',
  NOW() - INTERVAL '24 hours'
FROM pipelines p WHERE p.slug = 'google-console-mcp'
LIMIT 1;

-- Task 9: Decision - Should we start Compliance GRC MCP?
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  '99000000-0000-0000-0000-000000000009'::uuid,
  p.id,
  'intake',
  'decision',
  'Decision: Start Compliance GRC MCP Server?',
  'Compliance GRC (Governance, Risk, Compliance) MCP has been proposed. This would be our first enterprise compliance tool. Evaluate market demand, technical complexity, and available compliance APIs before committing resources.',
  '{"market_research_needed": true, "complexity_level": "high", "potential_platforms": ["OneTrust", "LogicGate", "NAVEX"], "decision_criteria": ["market_demand", "api_availability", "enterprise_readiness"]}'::jsonb,
  'pending',
  'high',
  true,
  NOW() + INTERVAL '72 hours',
  NOW() - INTERVAL '2 hours'
FROM pipelines p WHERE p.slug = 'compliance-grc-mcp'
LIMIT 1;

-- Task 10: Decision - Should we start HR People Ops MCP?
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  'aa000000-0000-0000-0000-00000000000a'::uuid,
  p.id,
  'intake',
  'decision',
  'Decision: Proceed with HR People Ops MCP?',
  'HR People Ops MCP would cover employee management, onboarding, time tracking, and performance reviews. We already have BambooHR and Rippling in progress. Decide if a generic People Ops server adds value or creates overlap.',
  '{"overlap_concern": true, "existing_hr_servers": ["bamboohr", "rippling", "gusto"], "proposed_unique_value": "unified_hr_interface", "decision_needed": "build_vs_consolidate"}'::jsonb,
  'pending',
  'medium',
  true,
  NOW() + INTERVAL '96 hours',
  NOW() - INTERVAL '1 hour'
FROM pipelines p WHERE p.slug = 'hr-people-ops-mcp'
LIMIT 1;

-- Task 11: Decision - Should we start Product Analytics MCP?
INSERT INTO tasks (
  id, pipeline_id, stage_name, type, title, description, context, status, priority,
  blocks_stage_advance, sla_deadline, created_at
)
SELECT
  'bb000000-0000-0000-0000-00000000000b'::uuid,
  p.id,
  'intake',
  'decision',
  'Decision: Build Product Analytics MCP Server?',
  'Product Analytics MCP would integrate with platforms like Mixpanel, Amplitude, PostHog, and Heap. High developer demand. Evaluate if this aligns with our SMB/mid-market focus or if we should prioritize other verticals.',
  '{"target_platforms": ["mixpanel", "amplitude", "posthog", "heap"], "market_demand": "high", "audience_fit": "developer_tools", "strategic_question": "smb_vs_developer_focus"}'::jsonb,
  'pending',
  'high',
  true,
  NOW() + INTERVAL '48 hours',
  NOW() - INTERVAL '30 minutes'
FROM pipelines p WHERE p.slug = 'product-analytics-mcp'
LIMIT 1;

COMMIT;

-- ============================================================================
-- Verification Queries (commented out, use separately if needed)
-- ============================================================================

-- SELECT id, name, type, status, tasks_completed_total FROM agents ORDER BY name;
-- SELECT id, title, type, status, priority FROM tasks ORDER BY created_at DESC;
