"""Original conceptual evidence diagram; no biological data or simulation."""
from pathlib import Path
import csv
import json
import textwrap
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch

OUT = Path(__file__).resolve().parent
ROWS = [
    ('sequenceidentity', 'Sequence identity', 'Do the measured letters agree with the stated reference?', 'Agreement is limited to the region examined and its uncertainty.'),
    ('assemblyscope', 'Assembly scope', 'Are the expected regions and their arrangement represented?', 'A digital assembly is a representation, not a physically synthesized genome.'),
    ('functioncontext', 'Function in context', 'What biological activity is supported, and in which context?', 'A predicted function and an observed function are different evidence claims.'),
    ('observedoutcome', 'Observed outcome', 'What trait or outcome was observed, and under what conditions?', 'An observation does not guarantee the same outcome in another setting.'),
]
SOURCES = [
    'https://www.genome.gov/genetics-glossary/DNA-Sequencing',
    'https://www.genome.gov/about-genomics/educational-resources/fact-sheets/human-genome-project',
    'https://pmc.ncbi.nlm.nih.gov/articles/PMC9186530/',
    'https://www.ncbi.nlm.nih.gov/refseq/functionalelements/',
    'https://www.genome.gov/genetics-glossary/Phenotype',
]
plt.rcParams.update({'font.family': 'DejaVu Sans', 'svg.fonttype': 'none', 'pdf.fonttype': 42})

def wrapped(text, width):
    return '\n'.join(textwrap.wrap(text, width))

def figure(mobile=False):
    fig = plt.figure(figsize=(6, 13) if mobile else (14, 7.5), facecolor='#f7f5ef')
    ax = fig.add_axes([0, 0, 1, 1]); ax.set_xlim(0, 1); ax.set_ylim(0, 1); ax.axis('off')
    ax.text(.05, .955, 'EVIDENCE LITERACY', fontsize=10, color='#43645f', weight='bold', va='top')
    ax.text(.05, .918, 'Four different questions,\nfour evidence needs' if mobile else 'Four different questions, four evidence needs', fontsize=22 if mobile else 24, weight='bold', color='#173c38', va='top')
    positions = [(0.05, .649-i*.174, .90, .158) for i in range(4)] if mobile else [(0.05+(i%2)*.46, .50-(i//2)*.31, .44, .275) for i in range(4)]
    for (_, title, question, limit), (x,y,w,h) in zip(ROWS, positions):
        ax.add_patch(FancyBboxPatch((x,y),w,h,boxstyle='round,pad=0.008,rounding_size=0.009',facecolor='#ffffff',edgecolor='#9bb3ab',linewidth=1.2))
        ax.text(x+.025,y+h-.018 if mobile else y+h-.025,title,fontsize=14,weight='bold',color='#173c38',va='top')
        ax.text(x+.025,y+h-.052 if mobile else y+h-.084,wrapped(question,44 if mobile else 57),fontsize=12 if mobile else 13,color='#202d2b',va='top',linespacing=1.45)
        ax.text(x+.025,y+.052 if mobile else y+.088,wrapped(limit,49 if mobile else 65),fontsize=10.5 if mobile else 11.5,color='#455852',va='top',linespacing=1.4)
    caption='Evidence for one question does not automatically answer the others.'
    ax.text(.05,.10 if mobile else .12,wrapped(caption,53) if mobile else caption,fontsize=11 if mobile else 13,color='#173c38',weight='bold',va='top',linespacing=1.4)
    ax.text(.05,.048 if mobile else .065,'Conceptual diagram | No outcome prediction\nSources and boundaries: methods.json',fontsize=9,color='#455852',va='top',linespacing=1.5)
    stem=OUT / ('questions-mobile' if mobile else 'questions-wide')
    for ext in ['svg','pdf','png']: fig.savefig(stem.with_suffix('.'+ext), dpi=150, facecolor=fig.get_facecolor())
    plt.close(fig)

if __name__=='__main__':
    with (OUT/'questions.csv').open('w',newline='',encoding='utf-8') as f:
        w=csv.writer(f); w.writerow(['evidence_label','panel_title','question','limitation']); w.writerows(ROWS)
    methods={'title':'Four different questions, four evidence needs','kind':'original conceptual evidence diagram','created':'2026-10-02','numeric_data':False,'simulation':False,'panels':'four equal panels, no arrows or progress score','outputs':['questions-wide.svg','questions-wide.pdf','questions-mobile.svg','questions-mobile.pdf'],'sources':SOURCES,'exclusions':['biological sequences','laboratory protocols','construct design','reproductive procedures','biological outcome simulation','archival proposal validation'],'boundary':'A digital assembly is a representation, not a physically synthesized genome.','reproduction':'python create_figures.py; requires matplotlib'}
    (OUT/'methods.json').write_text(json.dumps(methods,indent=2)+'\n',encoding='utf-8')
    figure(); figure(True)
