import json

# Load the scored data
with open(r'C:\temp\final_scores.json', encoding='utf-8') as f:
    data = json.load(f)

pairs = data['pairs']
scores = data['scores']

# Build final output format
output_pairs = []
for i, (pair, score) in enumerate(zip(pairs, scores)):
    entry = {
        'a': pair['a'],
        'b': pair['b'],
        'score': score
    }
    
    # Add explanation only for scores >= 40
    if score >= 40:
        # Generate brief explanation based on score
        if score >= 80:
            entry['why'] = 'Near-indistinguishable promises or very similar scope'
        elif score >= 60:
            entry['why'] = 'Promises genuinely overlap, easy to swap'
        elif score >= 40:
            entry['why'] = 'Adjacent promises with plausible misassignment risk'
    
    output_pairs.append(entry)

# Print summary
print(f"Total pairs: {len(output_pairs)}")
print(f"Score distribution:")
score_ranges = {
    '0-19': 0, '20-39': 0, '40-59': 0, '60-79': 0, '80-100': 0
}
for e in output_pairs:
    s = e['score']
    if s <= 19:
        score_ranges['0-19'] += 1
    elif s <= 39:
        score_ranges['20-39'] += 1
    elif s <= 59:
        score_ranges['40-59'] += 1
    elif s <= 79:
        score_ranges['60-79'] += 1
    else:
        score_ranges['80-100'] += 1

for r, count in score_ranges.items():
    print(f"  {r}: {count}")

# Save final output
final_output = {
    'evaluated': len(output_pairs),
    'pairs': output_pairs
}

with open(r'C:\temp\structured_output.json', 'w', encoding='utf-8') as f:
    json.dump(final_output, f, ensure_ascii=False, indent=2)

print("\nSaved structured output")
print("All pairs included in file order: YES")
