#!/usr/bin/env bun
/**
 * Weekly PR Summaries Skill
 * Proposes and executes a plan to generate weekly PR activity summaries
 * for contributors to zocomputer/hostagent.
 */

const PLAN_BRIEF = `**Goal:** Generate a summary of all PR activity from the past week for each contributor to the zocomputer/hostagent repository.

**Steps:**
1. Use the authenticated \`gh\` CLI to list all contributors to \`zocomputer/hostagent\` who had commits in the past 7 days
2. For each contributor from step 1, dynamically spawn a parallel work unit that extracts all their PRs from the past week and summarizes them

**Approach:**
- b1 filters for only contributors with recent commit activity (last 7 days)
- b2 is a dynamic bead that spawns child beads in parallel, one per contributor
- Each child bead extracts PR details (title, link, files changed, description) and generates its own markdown summary

**Expected Output:**
- Multiple markdown files/documents, one per contributor with recent activity
- Each contains PR summaries: titles, links, file changes, and descriptions

**Constraints:**
- Only use the already-authenticated gh CLI
- Focus on the past 7 days of activity
- Use parallel processing across contributors`;

async function proposeAndExecute() {
  console.log("📋 Proposing weekly PR summaries plan...");
  
  // Call the Zo API to propose the plan
  const proposeResponse = await fetch("https://api.zo.computer/zo/propose_plan", {
    method: "POST",
    headers: {
      "authorization": process.env.ZO_CLIENT_IDENTITY_TOKEN || "",
      "content-type": "application/json"
    },
    body: JSON.stringify({ brief: PLAN_BRIEF })
  });
  
  if (!proposeResponse.ok) {
    const error = await proposeResponse.text();
    console.error("Failed to propose plan:", error);
    process.exit(1);
  }
  
  const planResult = await proposeResponse.json();
  const planId = planResult.plan_id;
  
  console.log(`✅ Plan proposed: ${planId}`);
  console.log("🚀 Executing plan...");
  
  // Execute the plan
  const executeResponse = await fetch("https://api.zo.computer/zo/execute_plan", {
    method: "POST",
    headers: {
      "authorization": process.env.ZO_CLIENT_IDENTITY_TOKEN || "",
      "content-type": "application/json"
    },
    body: JSON.stringify({ plan_id: planId })
  });
  
  if (!executeResponse.ok) {
    const error = await executeResponse.text();
    console.error("Failed to execute plan:", error);
    process.exit(1);
  }
  
  console.log(`✅ Plan execution started: ${planId}`);
  console.log("\nMonitor progress at: /?t=plans");
  console.log(`Plan ID: ${planId}`);
}

proposeAndExecute().catch(console.error);
