#!/usr/bin/env node
import'./chunk-XHJ27CER.js';import*as d from'fs';import*as h from'path';import n from'chalk';import*as w from'js-yaml';function j(o){return h.join(o,".paradigm","personas")}function b(o){let i=j(o);return d.existsSync(i)?d.readdirSync(i).filter(s=>s.endsWith(".persona")).sort().map(s=>{try{let e=d.readFileSync(h.join(i,s),"utf8");return w.load(e)}catch{return null}}).filter(s=>s!==null&&!!s.id):[]}function m(o,i){let s=h.join(j(o),`${i}.persona`);if(!d.existsSync(s))return null;try{return w.load(d.readFileSync(s,"utf8"))}catch{return null}}function R(o){let i=h.join(j(o),"index.yaml");if(!d.existsSync(i))return null;try{return w.load(d.readFileSync(i,"utf8"))}catch{return null}}async function k(o){let i=process.cwd(),s=b(i);if(s.length===0){console.log(n.yellow(`
  No personas found. Create one with paradigm_persona_create.
`));return}if(o.tag&&(s=s.filter(e=>e.tags?.includes(o.tag))),o.trigger&&(s=s.filter(e=>e.trigger.type===o.trigger)),o.gate&&(s=s.filter(e=>e.journey.some(l=>l.gates.includes(o.gate)))),o.json){console.log(JSON.stringify(s.map(e=>({id:e.id,name:e.name,trigger:e.trigger.type,steps:e.journey.length,tags:e.tags||[],gates:[...new Set(e.journey.flatMap(l=>l.gates))]})),null,2));return}console.log(n.blue(`
  Personas (${s.length})
`));for(let e of s){let l=[...new Set(e.journey.flatMap(a=>a.gates))],t=e.trigger.type==="root"?n.green:n.cyan;console.log(`  ${n.white.bold(e.id)} \u2014 ${e.name}`),console.log(`    ${t(e.trigger.type)} trigger, ${e.journey.length} steps, ${l.length} gates`),e.tags?.length&&console.log(`    ${n.gray(e.tags.map(a=>`[${a}]`).join(" "))}`),e.description&&console.log(`    ${n.gray(e.description)}`),console.log();}}async function O(o,i){let s=process.cwd(),e=m(s,o);if(!e){console.log(n.red(`Persona "${o}" not found.`));return}if(i.json){console.log(JSON.stringify(e,null,2));return}if(console.log(n.blue(`
  Persona: ${n.white.bold(e.name)}
`)),console.log(`  ID:       ${e.id}`),console.log(`  Trigger:  ${e.trigger.type}`),e.trigger.spawned_by&&console.log(`  Spawned:  by ${e.trigger.spawned_by}`),e.description&&console.log(`  Desc:     ${e.description}`),e.tags?.length&&console.log(`  Tags:     ${e.tags.join(", ")}`),e.traits&&Object.keys(e.traits).length){console.log(n.cyan(`
  Traits:`));for(let[l,t]of Object.entries(e.traits))console.log(`    ${l}: ${n.white(String(t))}`);}if(e.fixtures&&Object.keys(e.fixtures).length){console.log(n.cyan(`
  Fixtures:`));for(let[l,t]of Object.entries(e.fixtures))console.log(`    ${l}: ${n.gray(t)}`);}console.log(n.cyan(`
  Journey (${e.journey.length} steps):`));for(let l=0;l<e.journey.length;l++){let t=e.journey[l],a=t.gates.map(r=>n.yellow(r)).join(", ");console.log(`    ${n.gray(`${l+1}.`)} ${n.white(t.id)} \u2014 ${t.route}`),console.log(`       Gates: ${a}  Expect: ${t.expect.status}`),t.flow&&console.log(`       Flow: ${n.magenta(t.flow)}`),t.produces&&console.log(`       Produces: ${Object.keys(t.produces).join(", ")}`),t.spawns?.length&&console.log(`       Spawns: ${t.spawns.map(r=>r.persona).join(", ")}`),t.signals?.length&&console.log(`       Signals: ${t.signals.join(", ")}`);}console.log();}async function C(o,i){let s=process.cwd(),e=o?[m(s,o)].filter(Boolean):b(s);if(e.length===0){console.log(n.yellow(o?`Persona "${o}" not found.`:"No personas found."));return}let l=[],t=h.join(s,"portal.yaml");if(d.existsSync(t))try{let g=w.load(d.readFileSync(t,"utf8"));l=Object.keys(g?.gates||{}).map(c=>c.startsWith("^")?c:`^${c}`);}catch{}let a=0,r=0,f=[];for(let g of e){let c=[],$=[];g.id||c.push("Missing required field: id"),g.name||c.push("Missing required field: name"),(!g.journey||g.journey.length===0)&&c.push("Journey must have at least 1 step");let p=new Set;for(let u of g.journey||[]){if(u.id||c.push("Step missing id"),p.has(u.id)&&c.push(`Duplicate step id: ${u.id}`),p.add(u.id),u.route||c.push(`Step ${u.id}: missing route`),(!u.gates||u.gates.length===0)&&c.push(`Step ${u.id}: gates[] required`),u.expect?.status||c.push(`Step ${u.id}: expect.status required`),l.length>0)for(let y of u.gates||[])l.includes(y)||$.push(`Step ${u.id}: gate ${y} not in portal.yaml`);for(let y of u.spawns||[])m(s,y.persona)||$.push(`Step ${u.id}: spawns ${y.persona} but that persona doesn't exist`);}a+=c.length,r+=$.length,f.push({id:g.id,errors:c,warnings:$});}if(i?.json){console.log(JSON.stringify({results:f,totalErrors:a,totalWarnings:r},null,2));return}console.log(n.blue(`
  Persona Validation (${e.length} persona(s))
`));for(let g of f)if(g.errors.length===0&&g.warnings.length===0)console.log(`  ${n.green("\u2713")} ${g.id}`);else {if(g.errors.length>0){console.log(`  ${n.red("\u2717")} ${g.id} \u2014 ${g.errors.length} error(s)`);for(let c of g.errors)console.log(`    ${n.red("\u2022")} ${c}`);}if(g.warnings.length>0){console.log(`  ${n.yellow("\u26A0")} ${g.id} \u2014 ${g.warnings.length} warning(s)`);for(let c of g.warnings)console.log(`    ${n.yellow("\u2022")} ${c}`);}}console.log(),console.log(a===0&&r===0?n.green("  All personas valid."):`  ${n.red(`${a} error(s)`)}, ${n.yellow(`${r} warning(s)`)}`),console.log();}async function N(o){let i=process.cwd(),s=R(i);if(!s){console.log(n.yellow(`
  No persona index found. Run paradigm_reindex first.
`));return}if(o.json){console.log(JSON.stringify({gate_coverage:s.gate_coverage||{},route_coverage:s.route_coverage||{},uncovered_routes:s.uncovered_routes||[]},null,2));return}console.log(n.blue(`
  Persona Coverage Report
`));let e=s.gate_coverage||{};if(Object.keys(e).length>0){console.log(n.cyan("  Gate Coverage:"));for(let[a,r]of Object.entries(e))console.log(`    ${n.yellow(a)} \u2014 ${r.join(", ")}`);console.log();}let l=s.route_coverage||{};if(Object.keys(l).length>0){console.log(n.cyan("  Route Coverage:"));for(let[a,r]of Object.entries(l))console.log(`    ${n.white(a)} \u2014 ${r.join(", ")}`);console.log();}let t=s.uncovered_routes||[];if(t.length>0){console.log(n.red(`  Uncovered Routes (${t.length}):`));for(let a of t)console.log(`    ${n.red("\u2022")} ${a}`);console.log();}else console.log(n.green("  All portal.yaml routes have persona coverage.")),console.log();}async function D(o,i){let s=process.cwd(),e=m(s,o);if(!e){console.log(n.red(`Persona "${o}" not found.`));return}if(i.dryRun){console.log(n.blue(`
  Dry Run: ${e.name}
`));for(let r=0;r<e.journey.length;r++){let f=e.journey[r];console.log(`  ${n.gray(`${r+1}.`)} ${f.route}`),console.log(`     Gates: ${f.gates.join(", ")}`),console.log(`     Expect: ${f.expect.status}`),f.produces&&console.log(`     Produces: ${Object.keys(f.produces).join(", ")}`);}console.log(n.gray(`
  (dry run \u2014 no HTTP requests made)
`));return}console.log(n.blue(`
  Running: ${e.name} \u2192 ${i.baseUrl}
`));let l={fixtures:e.fixtures||{},produces:{},context:e.trigger.context||{},env:process.env},t=0,a=0;for(let r of e.journey){let f=`${i.baseUrl}${r.route.split(" ").pop()}`,g=r.route.split(" ")[0],c;r.payload&&(c=JSON.stringify(r.payload).replace(/\{\{(\w+)\.(\w+)\}\}/g,($,p,u)=>{let y=l[p];return y?.[u]!==void 0?String(y[u]):`{{${p}.${u}}}`})),process.stdout.write(`  ${n.gray(`${r.id}:`)} ${g} ${f} ... `);try{let $={"Content-Type":"application/json",...r.headers||{}},p=await fetch(f,{method:g,headers:$,body:c});if(p.status===r.expect.status){if(console.log(n.green(`${p.status} \u2713`)),t++,r.produces)try{let u=await p.json();for(let[y,x]of Object.entries(r.produces)){let S=x.split(".").reduce((P,v)=>P?.[v],u);l.produces[y]=S;}}catch{}}else console.log(n.red(`${p.status} \u2717 (expected ${r.expect.status})`)),a++;}catch($){console.log(n.red(`ERROR: ${$.message}`)),a++;}}console.log(),console.log(`  ${n.green(`${t} passed`)}, ${a>0?n.red(`${a} failed`):n.green("0 failed")}`),console.log();}async function J(o,i){let s=process.cwd(),e=b(s),l=[];for(let t of e){let a=t.journey.filter(r=>r.gates.includes(o)||r.flow===o||r.signals?.includes(o));a.length>0&&l.push({id:t.id,name:t.name,steps:a.map(r=>r.id)});}if(i.json){console.log(JSON.stringify({symbol:o,affected:l},null,2));return}if(l.length===0){console.log(n.gray(`
  No personas reference ${o}.
`));return}console.log(n.blue(`
  Personas affected by ${n.yellow(o)}:
`));for(let t of l)console.log(`  ${n.white.bold(t.id)} \u2014 ${t.name}`),console.log(`    Steps: ${t.steps.join(", ")}`);console.log();}async function A(o){let i=process.cwd(),s=h.join(j(i),`${o}.persona`);if(!d.existsSync(s)){console.log(n.red(`Persona "${o}" not found.`));return}let l=b(i).filter(t=>t.id!==o&&t.journey.some(a=>a.spawns?.some(r=>r.persona===o)));l.length>0&&(console.log(n.yellow(`
  Warning: ${o} is spawned by: ${l.map(t=>t.id).join(", ")}`)),console.log(n.yellow(`  Deleting it will break their spawn chains.
`))),d.unlinkSync(s),console.log(n.green(`  Deleted persona "${o}".`));}export{J as personaAffectedCommand,N as personaCoverageCommand,A as personaDeleteCommand,k as personaListCommand,D as personaRunCommand,O as personaShowCommand,C as personaValidateCommand};