"""Independent WCAG reference values. Run manually, never regenerate during tests.

Uses Python Decimal (50-digit precision) without importing the application.
Source: W3C WCAG 2.2 relative luminance / contrast ratio definitions.
Color Match-authored inputs, explanations and generator: CC BY 4.0.
"""
from decimal import Decimal, getcontext
from pathlib import Path
import json
import argparse

getcontext().prec = 50
D = Decimal
samples = [
    ('000000', 'FFFFFF', 4.5, 'Black on white maximum'),
    ('FFFFFF', '000000', 4.5, 'Reversed maximum'),
    ('000000', '000000', 4.5, 'Identical black'),
    ('808080', '808080', 4.5, 'Identical gray'),
    ('FFFFFF', 'FFFFFF', 4.5, 'Identical white'),
    ('777777', 'FFFFFF', 4.5, 'Just below normal-text AA'),
    ('767676', 'FFFFFF', 4.5, 'Just above normal-text AA'),
    ('595959', 'FFFFFF', 7, 'Above normal-text AAA'),
    ('5A5A5A', 'FFFFFF', 7, 'Below normal-text AAA'),
    ('949494', 'FFFFFF', 3, 'Above the 3:1 boundary'),
    ('959595', 'FFFFFF', 3, 'Below the 3:1 boundary'),
    ('008080', 'FFFFFF', 4.5, 'CSS teal and white'),
    ('40E0D0', 'FFFFFF', 4.5, 'CSS turquoise and white'),
    ('000000', '40E0D0', 4.5, 'Black label on turquoise'),
    ('FFFF00', 'FFFFFF', 4.5, 'White label on yellow fails'),
    ('FAFAFA', 'FFFF00', 4.5, 'Near-white framework foreground fails'),
    ('000000', 'FFFF00', 4.5, 'Black label on yellow'),
    ('FF0000', 'FFFFFF', 4.5, 'Pure red and white'),
    ('0000FF', 'FFFFFF', 7, 'Pure blue and white'),
    ('00FF00', '000000', 4.5, 'Pure green and black'),
    ('3366CC', '000000', 3, 'G183 example link versus body'),
    ('3366CC', 'FFFFFF', 4.5, 'G183 example link versus background'),
    ('93C5FD', '111827', 4.5, 'Dark-mode link and background'),
    ('F9FAFB', '111827', 7, 'Dark-mode body and background'),
    ('0A0A0A', '000000', 4.5, 'Below sRGB transfer breakpoint'),
    ('0B0B0B', '000000', 4.5, 'Above sRGB transfer breakpoint'),
    ('010203', '040506', 4.5, 'Near-black channels'),
    ('FFF7FA', '382B43', 7, 'Pastel collection reading pair'),
]

def luminance(value):
    channels = [D(int(value[i:i+2], 16)) / D(255) for i in (0, 2, 4)]
    linear = [c / D('12.92') if c <= D('0.04045') else ((c + D('0.055')) / D('1.055')) ** D('2.4') for c in channels]
    return sum(c * w for c, w in zip(linear, map(D, ('0.2126', '0.7152', '0.0722'))))

records = []
for index, (fg, bg, target, label) in enumerate(samples, 1):
    a, b = luminance(fg), luminance(bg)
    ratio = (max(a, b) + D('0.05')) / (min(a, b) + D('0.05'))
    records.append(dict(id=f'contrast-{index:02}', label=label, foreground='#'+fg, background='#'+bg,
                        target=target, expected=float(ratio), expectedPass=ratio >= D(str(target))))
parser = argparse.ArgumentParser(description='Generate independent contrast references; stdout by default.')
parser.add_argument('--output', type=Path, help='Optional output JSON file; parent directory must exist.')
args = parser.parse_args()
payload = json.dumps(records, indent=2) + '\n'
if args.output:
    args.output.write_text(payload, encoding='utf-8')
else:
    print(payload, end='')
