"""Publish the consumed-development species/component source gallery."""
import html
import json
from pathlib import Path

R = Path(__file__).resolve().parents[1]
O = R / 'results/r112/gunduz-species-assets'
esc = html.escape


def main():
    assets = json.loads((O / 'assets.json').read_text())
    check = json.loads((O / 'verify.json').read_text())
    cards = []
    for x in assets['records']:
        cards.append('<article><h3><i>' + esc(x['source_name']) + '</i> · ' + esc(x['id']) + '</h3><p>Source image ' + esc(x['source_image_id']) + ' · source box ' + str(x['box_index']) + ' · component ' + str(x['component_area_px']) + ' pixels</p><div class="views"><figure><a href="' + esc(x['image']) + '"><img loading="lazy" src="' + esc(x['image']) + '" alt="Grayscale source crop"></a><figcaption>Native grayscale crop</figcaption></figure><figure><a href="' + esc(x['overlay']) + '"><img loading="lazy" src="' + esc(x['overlay']) + '" alt="Source component overlay"></a><figcaption>Source binary component overlaid</figcaption></figure><figure><a href="' + esc(x['mask']) + '"><img loading="lazy" src="' + esc(x['mask']) + '" alt="Source component mask"></a><figcaption>Extracted component</figcaption></figure></div></article>')
    page = '''<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Species-labeled source components</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}.views{display:grid;grid-template-columns:repeat(3,1fr);gap:8px}figure{margin:0}.views img{width:100%;height:230px;object-fit:contain;background:#e7ece7}figcaption{font-size:.85rem}@media(max-width:600px){.views{grid-template-columns:1fr}}</style><main><nav><a href="/results/r112/">Synthetic training</a> · <a href="/results/r115/">Prior Gündüz comparison</a></nav><h1>Species-labeled source components</h1><p class="notice"><strong>Consumed development candidates, not an independent test or approved ground truth.</strong> Source species names are linked to components by matching nonintersecting annotation boxes to connected binary-mask regions. This structural rule can make mistakes and does not establish biological identity, completeness or sample independence. Original images and names require review.</p><p>''' + str(check['components_checked']) + ''' native-resolution grayscale crops from ''' + str(check['source_images']) + ''' previously used source images cover ''' + str(check['source_name_strings']) + ''' literal source-name strings. The accompanying <a href="https://peerj.com/articles/cs-1970/">dataset paper</a> reports expert identification and pixel-level annotations; our box-to-component links and taxonomy have not been independently adjudicated. All source pixel sizes are unknown, so these assets are not physically matched to Academy fields. Crops may contain other organisms, and the background is unknown.</p><p>The source is <a href="https://www.kaggle.com/datasets/huseyingunduz/diatom-dataset">Hüseyin Gündüz's Diatom Dataset</a>, licensed CC BY-NC-SA 4.0; attribution, noncommercial and share-alike terms apply. The 20 images were already evaluated in R115 and cannot serve as an independent test. A coarse duplicate screen against earlier Khanom crops is not proof of independence.</p><p><a href="protocol.json">Selection and provenance protocol</a> · <a href="assets.json">All records</a> · <a href="verify.json">Reconstruction checks</a> · <a href="/diatom_pilot/gunduz_species_asset_dataset.py">Python development reader</a></p>''' + ''.join(cards) + '</main></html>'
    (O / 'index.html').write_text(page)
    print('published', len(cards), 'candidate species components')


if __name__ == '__main__':
    main()
