"""Publish paired original and outside-box prompt candidates."""
import html
import json
from pathlib import Path

R = Path(__file__).resolve().parents[1]
O = R / 'results/r112/outside-box-prompt-pilot'
esc = html.escape


def main():
    result = json.loads((O / 'results.json').read_text())
    verify = json.loads((O / 'verify.json').read_text())
    a, b = verify['summary']['high_coverage'], verify['summary']['reference']
    cards = []
    for x in result['records']:
        cards.append('<article><h3>' + esc(x['annotation_id']) + ' · ' + esc(x['source_name']) + '</h3><p>' + esc(x['group']) + ' · original ' + str(x['original_area_px']) + ' pixels · negative-prompt candidate ' + str(x['candidate_area_px']) + ' pixels · mask IoU ' + (f"{x['candidate_original_iou']:.3f}" if x['candidate_original_iou'] is not None else 'undefined') + '</p><a href="' + esc(x['comparison'], quote=True) + '"><img loading="lazy" src="' + esc(x['comparison'], quote=True) + '" alt="Source and two candidate mask overlays"></a></article>')
    page = '''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Outside-box prompt pilot</title><style>body{font:16px/1.5 system-ui;color:#193b37;background:#f2f6f2;margin:0}main{max-width:1200px;margin:auto;padding:24px}a{color:#076c59}.notice{background:#fff4d7;border-left:5px solid #b88418;padding:15px}article{background:white;border:1px solid #cbd8d1;padding:12px;margin:14px 0}img{width:100%;height:auto}table{border-collapse:collapse;background:white}td,th{padding:8px;border:1px solid #cbd8d1}</style><main><nav><a href="/results/r112/">Synthetic training</a> · <a href="/results/r112/breadth-extension-scenes/">Extension fields</a></nav><h1>Outside-box negative-point pilot</h1><p class="notice"><strong>This is not an approved mask improvement.</strong> The two known failure cases still select extensive background. All outputs are unverified model hypotheses, source rectangles are rough, and negative prompts can fall on other diatoms. No expert pixel truth or independent accuracy measurement is available.</p><p>For 24 deliberately selected high-coverage proposals and 12 reference proposals, the same MobileSAM model received the original positive center point plus up to four negative points outside the rough source rectangle. The first two cases were selected after spotting artifacts; other cases were chosen using fixed geometry and hash order. This is a small biased development comparison.</p><table><tr><th>Group</th><th>Cases</th><th>Candidate smaller</th><th>Candidate larger</th><th>Original &gt;75% of crop</th><th>Candidate &gt;75%</th></tr>''' + ''.join('<tr><th>' + label + '</th><td>' + str(s['cases']) + '</td><td>' + str(s['candidate_area_smaller']) + '</td><td>' + str(s['candidate_area_larger']) + '</td><td>' + str(s['original_coverage_gt_075']) + '</td><td>' + str(s['candidate_coverage_gt_075']) + '</td></tr>' for label, s in [('High coverage', a), ('Reference', b)]) + '''</table><p>Coverage and area changes do not establish biological accuracy: real diatoms can fill a crop, and a smaller mask may lose real structure. Three high-coverage candidates still contain a pixel at a negative prompt after output resizing; no strict point constraint is assumed.</p><p><a href="protocol.json">Frozen pilot protocol</a> · <a href="results.json">All records and masks</a> · <a href="verify.json">Geometry checks</a></p><p>Pink crosses mark the positive point; yellow crosses mark negative points. Blue indicates a proposed mask and orange its edge. Panels are resized for display; click for the saved comparison.</p>''' + ''.join(cards) + '</main></html>'
    (O / 'index.html').write_text(page)
    print('published', len(cards), 'paired comparisons')


if __name__ == '__main__':
    main()
