#!/usr/bin/env node
import*as s from'fs';import*as m from'path';import*as u from'js-yaml';var d=".paradigm/work-log";function p(a){let n=new Date().toISOString().slice(0,10),c=a.toLowerCase().replace(/[^a-z0-9-]/g,"-").slice(0,20),i=String(Math.floor(Math.random()*999)+1).padStart(3,"0");return `WL-${c}-${n}-${i}`}function k(a,o){return m.join(a,d,o)}function h(a,o){let n=new Date,c=n.toISOString().slice(0,10),i=p(o.agent),e={id:i,timestamp:n.toISOString(),...o},t=k(a,c);s.mkdirSync(t,{recursive:true});let r=m.join(t,`${i}.yaml`);return s.writeFileSync(r,u.dump(e,{lineWidth:120,noRefs:true}),"utf8"),e}function W(a,o){let n=m.join(a,d);if(!s.existsSync(n))return [];let c=[],i=s.readdirSync(n,{withFileTypes:true}).filter(t=>t.isDirectory()&&/^\d{4}-\d{2}-\d{2}$/.test(t.name)).map(t=>t.name).sort().reverse();for(let t of i){if(o?.dateFrom&&t<o.dateFrom||o?.dateTo&&t>o.dateTo)continue;let r=m.join(n,t),l=s.readdirSync(r).filter(g=>g.endsWith(".yaml"));for(let g of l)try{let y=s.readFileSync(m.join(r,g),"utf8"),f=u.load(y);f&&f.id&&c.push(f);}catch{}}let e=c;return o?.agent&&(e=e.filter(t=>t.agent===o.agent)),o?.outcome&&(e=e.filter(t=>t.outcome===o.outcome)),o?.task_ref&&(e=e.filter(t=>t.task_ref===o.task_ref)),o?.symbol&&(e=e.filter(t=>t.symbols_touched?.includes(o.symbol))),e.sort((t,r)=>r.timestamp.localeCompare(t.timestamp)),o?.limit&&(e=e.slice(0,o.limit)),e}function S(a,o){let n=m.join(a,d);if(!s.existsSync(n))return null;let c=o.match(/WL-[^-]+-(\d{4}-\d{2}-\d{2})-/);if(c){let e=m.join(n,c[1],`${o}.yaml`);if(s.existsSync(e)){let t=s.readFileSync(e,"utf8");return u.load(t)}}let i=s.readdirSync(n,{withFileTypes:true}).filter(e=>e.isDirectory());for(let e of i){let t=m.join(n,e.name,`${o}.yaml`);if(s.existsSync(t)){let r=s.readFileSync(t,"utf8");return u.load(r)}}return null}function L(a,o=7){let n=new Date;n.setDate(n.getDate()-o);let c=n.toISOString().slice(0,10),i=W(a,{dateFrom:c}),e={pass:0,fail:0,partial:0,blocked:0},t={};for(let r of i)e[r.outcome]=(e[r.outcome]||0)+1,t[r.agent]=(t[r.agent]||0)+1;return {total:i.length,byOutcome:e,byAgent:t,recentEntries:i.slice(0,10).map(r=>({id:r.id,agent:r.agent,summary:r.summary,outcome:r.outcome,timestamp:r.timestamp}))}}export{h as a,W as b,S as c,L as d};