import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const dir=path.dirname(fileURLToPath(import.meta.url));
const data=JSON.parse(fs.readFileSync(path.join(dir,'observations.json'),'utf8'));
const rows=data.records;
const seen=new Set();
for(const r of rows){
 if(seen.has(r.observation_id)) throw new Error('Duplicate observation: '+r.observation_id);
 seen.add(r.observation_id);
 if(!['complete','missing'].includes(r.capture_status)) throw new Error('Unknown capture status');
 if(!['present','absent','not_assessable'].includes(r.ai_answer_state)) throw new Error('Unknown answer state');
 if(r.capture_status==='missing' && r.ai_answer_state!=='not_assessable') throw new Error('Missing is not absence');
 if(!Number.isFinite(Date.parse(r.observed_at_utc))) throw new Error('Invalid timestamp');
}
for(const q of data.questions) for(const surface of data.surfaces) for(const round of [1,2,3]){
 const id=`${q.id}-${surface}-r${round}`;
 if(!seen.has(id)) throw new Error('Missing planned row: '+id);
}
if(rows.length!==30) throw new Error('Expected 30 planned observations');
const starts=[1,2,3].map(round=>Math.min(...rows.filter(r=>r.round===round).map(r=>Date.parse(r.observed_at_utc))));
const spacing=starts.slice(1).map((t,i)=>(t-starts[i])/60000);
if(spacing.some(x=>x<20)) throw new Error('Round-start spacing below frozen protocol');
const result={planned:30,records:rows.length,complete:rows.filter(r=>r.capture_status==='complete').length,missing:rows.filter(r=>r.capture_status==='missing').length,by_surface:{},round_start_spacing_minutes:spacing};
for(const surface of data.surfaces){const s=rows.filter(r=>r.surface===surface);result.by_surface[surface]={observations:s.length,complete:s.filter(r=>r.capture_status==='complete').length,answer_present:s.filter(r=>r.ai_answer_state==='present').length,answer_absent:s.filter(r=>r.ai_answer_state==='absent').length};}
const claims=JSON.parse(fs.readFileSync(path.join(dir,'claims.json'),'utf8')).records;
const codes={supported:0,partial:0,contradicted:0,not_addressed:0,unverifiable:0};
const claimIds=new Set();
for(const claim of claims){
 if(claimIds.has(claim.claim_id)||!seen.has(claim.observation_id)) throw new Error('Invalid claim identity');
 claimIds.add(claim.claim_id);
 if(claim.selection!=='first_two_predefined'||!(claim.initial_support_code in codes)) throw new Error('Unexpected claim scope or support code');
 codes[claim.initial_support_code]++;
}
if(claims.length!==20) throw new Error('Expected twenty initial first-round claim occurrences');
result.initial_claim_annotations={claims:claims.length,support_counts:codes,independent_review:'not_performed',scope:'Support by inspected visible sources, not correctness or a population error rate.'};
result.visible_edikka_links={observations_with_link:rows.filter(r=>r.visible_source_urls.some(url=>{const host=new URL(url).hostname;return host==='edikka.com'||host.endsWith('.edikka.com');})).length,observations:rows.length,source_inventory_complete:rows.every(r=>r.source_set_complete),scope:'Recorded visible URLs only; no inference about selection bias or all citations.'};
console.log(JSON.stringify(result,null,2));
