#!/usr/bin/env python3
"""
Insert April 2026 blood test panel into Supabase.
Lab: Sullivan Nicolaides Pathology (SNP). Collected 24 Apr 2026, 09:35 fasting.
Source PDF: /Users/kimhansen/Downloads/BP2026050156741.pdf
"""
import sys, hashlib
sys.path.insert(0, '.claude/skills/supabase-connector')
from client import get_supabase_client

c = get_supabase_client()
PANEL_ID = 'bt202604'

# ---------- PANEL ----------
panel = {
    'id': PANEL_ID,
    'date': '2026-04-24',
    'lab_name': 'Sullivan Nicolaides Pathology',
    'ordered_by': 'Dr Mark J Friel',
    'panel_name': 'Comprehensive Quarterly',
    'analysis_summary': (
        'Major wins: lipid intervention worked. Triglycerides crashed 71% (1.4 -> 0.4), '
        'LDL now in range (4.1 -> 3.9), ApoB down 10% (1.12 -> 1.01), HDL up 8%. '
        'Homocysteine hit 8.4 (target <8 close). Testosterone +24% to 33.4. '
        'New concerns: Vitamin D 218 nmol/L (high, lab flags excessive supplementation -> stop standalone D3). '
        'Fasting insulin 2 (lab flag low, but HOMA-IR ~0.41 indicates excellent insulin sensitivity in context of HbA1c 5.4 + FG 4.6). '
        'ApoB 1.01 still above Attia longevity target (<0.65) - discuss next step with Dr Friel.'
    ),
    'health_score': 82,
    'markers_optimal': 41,
    'markers_good': 17,
    'markers_warning': 2,
    'markers_high': 2,
    'markers_low': 1,
    'context_snapshot': {
        'whoop_avg_recovery': 79,
        'whoop_avg_hrv': 53,
        'whoop_avg_sleep_hours': 7.5,
        'fasting': True,
        'fast_duration_hours': 14,
        'collection_time': '09:35',
        'haemolysis_index': 18,
        'lab_notes': 'LD and AST may be falsely elevated due to mild haemolysis. Vitamin D elevated due to excessive supplementation per lab.',
        'active_supplements_at_draw': [
            'Blueprint Essential Capsules (D3 2000 IU + NR 300mg + CoQ10 + zinc + B-vits)',
            'Blueprint Longevity Mix (creatine 2.5g + CaAKG + taurine + glycine)',
            'Standalone D3 + K2 MK-7 (2000 IU D3 + K2)',
            'Triple Strength Omega-3 (2,500mg+ EPA+DHA)',
            'Magnesium L-Threonate 2g (am) + Magnesium Glycinate 400mg (pm)',
            'Methylated B-complex (folate 800mcg + B12 1000mcg + P5P 50mg)',
            'Vitamin C 750mg, Creatine 2.5g standalone, Collagen 14.9g, Luteolin 100mg'
        ],
        'active_diet_changes': [
            'Coconut oil REMOVED from tea',
            'Eggs reduced 4 -> 1 daily in smoothie',
            'Fish primary dinner protein (salmon 3-4x/week)',
            'Red meat <=1x/week',
            'Soluble fiber added (oats 30g + psyllium 5g morning)',
            'Ground flaxseed 1 tbsp daily',
            'Legumes/lentils 3-4x/week',
            'Fat-free Greek yoghurt'
        ]
    },
    'source': 'manual',
    'notes': 'Auto-import from PDF. Dr Friel discussion pending. Lp(a) NOT retested (genetic, prior 72 nmol/L low risk).'
}

# ---------- MARKERS ----------
# (name, short_name, category, sort, value, unit, ref_low, ref_high, optimal_low, optimal_high, status, notes)
M = [
    # Lipids
    ('Cholesterol', 'TC', 'lipids', 1, 5.6, 'mmol/L', None, 5.6, None, 5.0,
     'borderline', 'At upper limit. Improved 6.1->5.6 with diet. Attia longevity prefers <5.0.'),
    ('Triglyceride', 'TG', 'lipids', 2, 0.4, 'mmol/L', None, 2.1, None, 0.85,
     'optimal', 'DRAMATIC drop 1.4->0.4. Omega-3 + diet working. Attia target <0.85.'),
    ('HDL', 'HDL', 'lipids', 3, 1.55, 'mmol/L', 0.89, None, 1.0, None,
     'optimal', 'Up 1.44->1.55. EVOO + nuts + fish working.'),
    ('LDL', 'LDL', 'lipids', 4, 3.9, 'mmol/L', None, 4.1, None, 2.6,
     'good', 'In range now (4.1->3.9). Attia longevity target <2.6.'),
    ('Chol/HDL Ratio', 'Chol/HDL', 'lipids', 5, 3.6, 'ratio', None, 4.6, None, 3.5,
     'good', 'Improved 4.2->3.6.'),
    ('Non HDLC', 'NonHDLC', 'lipids', 6, 4.05, 'mmol/L', None, 3.81, None, 3.4,
     'high', 'Improved 4.66->4.05 but still above NVDPA target. Most significant subclinical atherosclerosis marker.'),
    ('Apolipoprotein A1', 'ApoA1', 'lipids', 7, 1.61, 'g/L', 0.95, 1.86, 1.4, None,
     'good', 'Stable.'),
    ('Apolipoprotein B', 'ApoB', 'lipids', 8, 1.01, 'g/L', 0.49, 1.73, None, 0.65,
     'warning', '10% drop from 1.12. Lab range OK but Attia longevity target <0.65 (very high <0.65, high <0.80, moderate <1.0). Just above moderate.'),
    ('ApoB/ApoA1 Ratio', 'ApoB/A1', 'lipids', 9, 0.63, 'ratio', None, 0.9, None, 0.6,
     'good', 'Improved 0.69->0.63. Below high-risk 0.9.'),

    # Inflammation
    ('CRP', 'CRP', 'inflammation', 10, 0.9, 'mg/L', None, 5, None, 0.5,
     'good', 'Slight uptick 0.4->0.9 but still excellent. Attia elite tier <0.5.'),

    # Metabolic
    ('HbA1c (NGSP)', 'HbA1c', 'metabolic', 11, 5.4, '%', None, 6.5, None, 5.2,
     'good', 'Stable. Attia longevity <5.2; Bryan Johnson 4.8-5.0.'),
    ('HbA1c (IFCC)', 'HbA1c-IFCC', 'metabolic', 12, 36, 'mmol/mol', None, 48, None, 33,
     'good', 'IFCC equivalent.'),
    ('Fasting Glucose', 'FG', 'metabolic', 13, 4.6, 'mmol/L', 3.6, 6.0, 4.5, 5.5,
     'optimal', 'First fasting glucose result - excellent.'),
    ('Insulin (fasting)', 'Insulin', 'metabolic', 14, 2, 'mU/L', 3, 15, None, 5,
     'low', 'Lab flag LOW. HOMA-IR=(4.6*2)/22.5=0.41 indicates EXCELLENT insulin sensitivity. With HbA1c 5.4 + FG 4.6, beta cells producing adequately. Likely benign - context of fasting + exercise. Discuss with Dr Friel.'),
    ('Homocysteine', 'Hcy', 'metabolic', 15, 8.4, 'umol/L', 4, 15, None, 8,
     'good', 'Improved 10.0->8.4 with methylated B-complex. Patrick longevity target <8 - close.'),

    # Liver
    ('AST', 'AST', 'liver', 16, 34, 'U/L', 10, 40, None, 30,
     'good', 'Slight rise 26->34. Lab notes mild haemolysis (idx 18) may falsely elevate. Re-evaluate at next draw.'),
    ('ALT', 'ALT', 'liver', 17, 26, 'U/L', 5, 40, None, 20,
     'good', 'Rose 14->26 but still optimal range. Bryan Johnson Blueprint <20 ideal.'),
    ('GGT', 'GGT', 'liver', 18, 15, 'U/L', 5, 50, None, 20,
     'optimal', 'Pristine. Zero alcohol shows.'),
    ('ALP', 'ALP', 'liver', 19, 46, 'U/L', 35, 110, None, 80,
     'optimal', 'Stable.'),
    ('T Bilirubin', 'TBili', 'liver', 20, 14, 'umol/L', None, 21, None, 17,
     'optimal', 'Dropped from 16.'),
    ('LD', 'LD', 'liver', 21, 197, 'U/L', None, 250, None, 220,
     'good', 'Slight rise. Haemolysis affected.'),

    # Kidney
    ('Creatinine', 'Cr', 'kidney', 22, 90, 'umol/L', 60, 110, None, 100,
     'good', 'Stable. Creatine supplementation can elevate slightly.'),
    ('eGFR', 'eGFR', 'kidney', 23, 85, 'mL/min', 59, None, 90, None,
     'good', 'Stable 86->85. Recovered from 2024 dip (72) but not yet >90.'),
    ('Urea', 'Urea', 'kidney', 24, 4.2, 'mmol/L', 3.5, 8.5, None, 6,
     'optimal', 'BIG improvement 7.4->4.2. Reflects better hydration at draw + protein moderation.'),
    ('Urate', 'Urate', 'kidney', 25, 0.339, 'mmol/L', 0.200, 0.500, None, 0.4,
     'optimal', 'Stable mid-range.'),

    # Hormones
    ('Testosterone', 'T', 'hormones', 26, 33.4, 'nmol/L', 11.0, 40.0, 20, None,
     'optimal', 'BIG jump 27.0->33.4 (+24%). Top decile for age 51. Resistance training + sleep working.'),
    ('SHBG', 'SHBG', 'hormones', 27, 72, 'nmol/L', 10.0, 70.0, None, 50,
     'high', 'Slight improvement 76->72 but still above lab range. Driven by aging + low body fat + fasting.'),
    ('Calculated Free Testosterone', 'fT', 'hormones', 28, 336, 'pmol/L', 260, 740, 350, None,
     'good', 'Stable. Healthy bioactive fraction despite high SHBG.'),
    ('Oestradiol', 'E2', 'hormones', 29, 85, 'pmol/L', None, 165, 50, 150,
     'good', 'Now measurable (was <50). Healthier - some E2 needed for bone, vascular, cognition in males.'),

    # Thyroid
    ('TSH', 'TSH', 'hormones', 30, 0.9, 'mIU/L', 0.3, 4.0, 0.5, 2.0,
     'optimal', 'Excellent.'),
    ('Free T4', 'fT4', 'hormones', 31, 13.2, 'pmol/L', 9.0, 19.0, None, None,
     'optimal', 'New on this panel.'),
    ('Free T3', 'fT3', 'hormones', 32, 4.8, 'pmol/L', 3.0, 6.0, 4.0, 6.0,
     'optimal', 'New on this panel - upper-mid range, healthy thyroid conversion.'),
    ('Reverse T3', 'rT3', 'hormones', 33, 343, 'pmol/L', 140, 540, None, 350,
     'good', 'New on this panel. Mid-range. fT3:rT3 ratio = 14 (healthy).'),

    # Haematology
    ('Haemoglobin', 'Hb', 'haematology', 34, 143, 'g/L', 135, 175, 140, 160,
     'good', 'Dropped 156->143 - large change. Hydration at draw + possible mild dilutional. Still in range.'),
    ('Haematocrit', 'Hct', 'haematology', 35, 0.42, 'ratio', 0.40, 0.54, None, None,
     'good', 'Tracks Hb drop.'),
    ('RCC', 'RCC', 'haematology', 36, 4.8, '10*12/L', 4.5, 6.5, None, None,
     'good', 'Tracks Hb drop. All within range.'),
    ('MCV', 'MCV', 'haematology', 37, 87, 'fL', 80, 100, 85, 95,
     'optimal', 'Stable.'),
    ('WCC', 'WCC', 'haematology', 38, 4.7, '10*9/L', 3.5, 10.0, 4, 7,
     'optimal', 'Lower than Jan (7.9) - normal variation, well in range.'),
    ('Neutrophils', 'Neut', 'haematology', 39, 2.51, '10*9/L', 1.5, 6.5, None, None,
     'optimal', 'Lower (5.25->2.51). Normal variation.'),
    ('Lymphocytes', 'Lymph', 'haematology', 40, 1.68, '10*9/L', 1.0, 4.0, None, None,
     'optimal', 'Stable.'),
    ('Monocytes', 'Mono', 'haematology', 41, 0.32, '10*9/L', 0, 0.9, None, None,
     'optimal', 'Stable.'),
    ('Eosinophils', 'Eos', 'haematology', 42, 0.14, '10*9/L', 0, 0.6, None, None,
     'optimal', 'Stable.'),
    ('Basophils', 'Baso', 'haematology', 43, 0.04, '10*9/L', 0, 0.15, None, None,
     'optimal', 'Stable.'),
    ('Platelets', 'Plt', 'haematology', 44, 234, '10*9/L', 150, 400, None, None,
     'optimal', 'Dropped 282->234 but well in range.'),

    # Electrolytes / chemistry
    ('Sodium', 'Na', 'other', 45, 139, 'mmol/L', 135, 145, None, None, 'optimal', None),
    ('Potassium', 'K', 'other', 46, 4.4, 'mmol/L', 3.5, 5.5, None, None, 'optimal', None),
    ('Chloride', 'Cl', 'other', 47, 104, 'mmol/L', 95, 110, None, None, 'optimal', None),
    ('Bicarbonate', 'HCO3', 'other', 48, 26, 'mmol/L', 20, 32, None, None, 'optimal', None),
    ('Anion Gap', 'AnionGap', 'other', 49, 9, 'mmol/L', None, 16, None, None, 'optimal', None),
    ('Calcium (Corrected)', 'Ca', 'other', 50, 2.28, 'mmol/L', 2.10, 2.60, None, None,
     'optimal', 'Dropped 2.42->2.28 - reassuring given high Vit D.'),
    ('Phosphate', 'PO4', 'other', 51, 0.88, 'mmol/L', 0.80, 1.50, None, None,
     'good', 'Dropped 1.22->0.88. Low-end of normal.'),
    ('Magnesium', 'Mg', 'other', 52, 0.86, 'mmol/L', 0.70, 1.10, 0.85, 1.05,
     'optimal', 'New on this panel. Good - Mg supplementation working.'),

    # Iron / vitamins
    ('Iron', 'Fe', 'vitamins', 53, 15, 'umol/L', 5, 30, None, None, 'optimal', None),
    ('Transferrin', 'Tf', 'vitamins', 54, 2.6, 'g/L', 1.9, 3.1, None, None, 'optimal', None),
    ('TIBC', 'TIBC', 'vitamins', 55, 65, 'umol/L', 47, 77, None, None, 'optimal', None),
    ('Transferrin Saturation', 'TSat', 'vitamins', 56, 23, '%', 20, 45, 25, 35,
     'good', 'Lower end - acceptable.'),
    ('Ferritin', 'Ferritin', 'vitamins', 57, 104, 'ug/L', 30, 300, 50, 150,
     'optimal', 'Sweet spot - not too low, not high (high ferritin = inflammation).'),
    ('Vitamin B12', 'B12', 'vitamins', 58, 471, 'pmol/L', 150, None, 300, None,
     'optimal', 'Up 402->471. Methylated B-complex working.'),
    ('Folate (Serum)', 'Folate', 'vitamins', 59, 35, 'nmol/L', 7, None, 15, None,
     'optimal', 'High from supplementation.'),
    ('25-OH Vitamin D', 'VitD', 'vitamins', 60, 218, 'nmol/L', 50, 150, 100, 150,
     'high', 'LAB FLAG H. Up from 120 (May 2024). Lab comment: "elevated vitamin D, can be caused by excessive supplementation". Above Patrick (100-150) and Bryan Johnson (75-150) targets. Below toxicity threshold (>250). ACTION: stop standalone D3+K2 capsule; Blueprint Essentials still provides 2000 IU.'),

    # Prostate
    ('Total PSA', 'PSA', 'other', 61, 0.88, 'ug/L', None, 3.0, None, 1.0, 'optimal', 'Excellent. New baseline at age 51.'),
    ('Free PSA', 'fPSA', 'other', 62, 0.29, 'ug/L', None, None, None, None, 'optimal', None),
    ('Free PSA Percent', '%fPSA', 'other', 63, 33.0, '%', 25, None, None, None, 'optimal', '>25% = benign pattern.'),

    # Other
    ('Total Protein', 'TP', 'other', 64, 67, 'g/L', 63, 80, None, None,
     'good', 'Dropped 77->67 - notable but in range. Hydration effect at draw.'),
    ('Albumin', 'Alb', 'liver', 65, 39, 'g/L', 32, 44, 38, 44, 'optimal', None),
    ('Globulin', 'Glob', 'liver', 66, 28, 'g/L', 23, 43, None, None, 'optimal', None),
    ('Haemolysis Index', 'HI', 'other', 67, 18, 'mg/dL', None, 40, None, 10,
     'good', 'Mild haemolysis - affects AST/LD readings. Worth a clean draw next time.'),
]

# ---------- ACTIONS ----------
A = [
    {'title': 'STOP standalone Vitamin D3 + K2 (MK-7) capsule',
     'description': '25-OH Vit D 218 nmol/L is above Patrick (100-150) and Blueprint (75-150) targets. Lab specifically flagged "excessive supplementation". Pause the standalone D3+K2 capsule entirely - Blueprint Essentials still provides 2000 IU/day. Consider buying K2-only supplement if vascular calcification concern (~$20/mo on Amazon AU).',
     'category': 'supplement', 'priority': 'urgent',
     'related_markers': ['25-OH Vitamin D']},

    {'title': 'Retest Vitamin D in 8-12 weeks',
     'description': '25-OH-D half-life is 13-15 days; full re-equilibration takes 2-3 months. Target zone after stopping standalone D3: 100-150 nmol/L (Patrick longevity range).',
     'category': 'test', 'priority': 'high',
     'related_markers': ['25-OH Vitamin D']},

    {'title': 'Discuss ApoB strategy with Dr Friel',
     'description': 'ApoB 1.01 g/L is at the moderate-risk threshold. Diet intervention got 10% reduction (1.12->1.01) - validating direction. But Attia longevity target is <0.65 g/L. With Lp(a) 72 (genetic baseline) and family considerations, discuss: (1) continue diet 3 more months and retest, OR (2) add ezetimibe 10mg/day (low-side-effect, blocks absorption, typical -15-20% LDL/ApoB).',
     'category': 'doctor', 'priority': 'high',
     'related_markers': ['Apolipoprotein B', 'LDL', 'Non HDLC']},

    {'title': 'Request CAC scan + APOE genotype + MTHFR genotype',
     'description': 'CAC scan directly images coronary plaque burden - contextualizes ApoB 1.01 with real imaging (CAC=0 reassuring; >0 strengthens case for pharmacology). APOE genotype informs lifetime CV/Alzheimer risk strategy. MTHFR genotype confirms whether methylated B forms are specifically needed (homocysteine responded well, so likely yes).',
     'category': 'test', 'priority': 'high',
     'related_markers': ['Apolipoprotein B', 'Homocysteine']},

    {'title': 'Verify low fasting insulin (2 mU/L) with Dr Friel',
     'description': 'Lab flagged LOW. HOMA-IR = (4.6 * 2) / 22.5 = 0.41 - indicates excellent insulin sensitivity (optimal <1.0). With HbA1c 5.4 stable + FG 4.6, beta cells are clearly producing adequately - rules out beta-cell insufficiency. Likely genuine high sensitivity from intermittent fasting + low body fat + exercise. Confirm interpretation with Dr Friel; no action needed if confirmed benign.',
     'category': 'doctor', 'priority': 'medium',
     'related_markers': ['Insulin (fasting)', 'HbA1c (NGSP)', 'Fasting Glucose']},

    {'title': 'Continue current diet protocol - it WORKS',
     'description': 'Validated by results: TG -71%, LDL into range, ApoB -10%, HDL +8%, Homocysteine -16%, Testosterone +24%. Specifically: keep coconut oil OUT of tea, eggs at 1/day, fish 3-4x/week dinners, oats+psyllium morning, legumes 3-4x/week, ground flaxseed daily, fat-free Greek yoghurt.',
     'category': 'diet', 'priority': 'high',
     'related_markers': ['Triglyceride', 'LDL', 'Apolipoprotein B', 'HDL', 'Homocysteine']},

    {'title': 'Continue methylated B-complex (homocysteine 8.4 close to target)',
     'description': 'Improved 10.0->8.4 in 3 months. Patrick target <8. Keep current dose (folate 800mcg + B12 1000mcg + P5P 50mg) - retest in 3 months.',
     'category': 'supplement', 'priority': 'medium',
     'related_markers': ['Homocysteine']},

    {'title': 'Continue 2,500mg+ EPA+DHA omega-3',
     'description': 'TG dropped 71% - clear evidence omega-3 + diet are working. AHA prescription dose is 4g/day for hypertriglyceridemia; Kim already at 2.5g+. Optionally increase to 3g/day for further ApoB push. Add Omega-3 Index test next panel.',
     'category': 'supplement', 'priority': 'medium',
     'related_markers': ['Triglyceride', 'Apolipoprotein B']},

    {'title': 'Increase resistance training to 4x/week (still pending)',
     'description': 'Testosterone already +24% (27.0->33.4) on 3x/week. SHBG only marginally improved (76->72). Pushing to 4x/week per Huberman protocol (compound movements, 6 sets x 10 reps, 2-min rest, 70-90% effort) should drop SHBG further into range and continue testosterone trajectory.',
     'category': 'lifestyle', 'priority': 'medium',
     'related_markers': ['Testosterone', 'SHBG']},

    {'title': 'Add Omega-3 Index test to next panel',
     'description': 'Per Patrick - measures actual tissue omega-3 status. Target >8%. Validates fish oil + dietary fish absorption. Cannot be inferred from serum.',
     'category': 'test', 'priority': 'low',
     'related_markers': []},

    {'title': 'Schedule next quarterly retest for July-August 2026',
     'description': 'Repeat full panel + add: Omega-3 Index, retest Lp(a) every 5 years (next due 2031), confirm Vitamin D back in 100-150 nmol/L range, confirm ApoB trajectory continues.',
     'category': 'test', 'priority': 'medium',
     'related_markers': []},
]

# ---------- INSERT ----------
print("Upserting panel...")
c.table('chl_bloodtest_panels').upsert(panel).execute()

print(f"Deleting old markers for {PANEL_ID}...")
c.table('chl_bloodtest_markers').delete().eq('panel_id', PANEL_ID).execute()

print(f"Inserting {len(M)} markers...")
marker_rows = []
for (name, short, cat, sort, val, unit, rl, rh, ol, oh, status, notes) in M:
    h = hashlib.md5(f"{PANEL_ID}-{name}".encode()).hexdigest()[:8]
    row = {
        'id': h,
        'panel_id': PANEL_ID,
        'name': name,
        'short_name': short,
        'category': cat,
        'sort_order': sort,
        'value': val,
        'unit': unit,
        'ref_low': rl,
        'ref_high': rh,
        'optimal_low': ol,
        'optimal_high': oh,
        'status': status,
        'notes': notes,
    }
    marker_rows.append(row)

# Insert in chunks
for i in range(0, len(marker_rows), 25):
    chunk = marker_rows[i:i+25]
    c.table('chl_bloodtest_markers').insert(chunk).execute()
print(f"Inserted {len(marker_rows)} markers.")

print(f"Deleting old actions for {PANEL_ID}...")
c.table('chl_bloodtest_actions').delete().eq('panel_id', PANEL_ID).execute()

print(f"Inserting {len(A)} actions...")
action_rows = []
for i, a in enumerate(A):
    h = hashlib.md5(f"{PANEL_ID}-action-{i}-{a['title'][:20]}".encode()).hexdigest()[:8]
    row = dict(a)
    row['id'] = h
    row['panel_id'] = PANEL_ID
    row['status'] = 'pending'
    action_rows.append(row)
c.table('chl_bloodtest_actions').insert(action_rows).execute()
print(f"Inserted {len(action_rows)} actions.")

# ---------- SUPPLEMENT UPDATE: pause D3+K2 standalone ----------
print("\nPausing standalone Vitamin D3 + K2 supplement...")
c.table('chl_supplements').update({
    'status': 'paused',
    'discontinued_date': '2026-05-02',
    'discontinued_reason': 'Vit D 218 nmol/L (lab flag H, "excessive supplementation"). Pausing standalone capsule; Blueprint Essentials retains 2000 IU. Retest 8-12 weeks. Will source K2-only if needed.'
}).eq('id', 'ea2949b4').execute()
print("Paused.")

# ---------- BIOAGE LOG ----------
# Compute composite bio age. Use existing recent vascular_age and whoop_age, update domain scores.
print("\nLogging bioage entry for 2026-04-24...")
# Domain scores reflecting NEW April panel — keep keys consistent with existing convention
domain_scores = {
    'cardiovascular': 75,   # ApoB 1.01 (improving), CRP 0.9, HDL 1.55 — improved from ~65
    'metabolic': 88,         # HbA1c 5.4, FG 4.6, Insulin 2 (sensitivity excellent), Hcy 8.4
    'sleep_recovery': 82,    # WHOOP recovery 79, HRV 53, sleep 7.5h
    'inflammation': 88,      # CRP 0.9, Hcy 8.4, neutrophils 2.51 — all excellent
    'liver_kidney': 84,      # eGFR 85, ALT 26, GGT 15, urea 4.2
    'body_composition': 80,  # estimated (Withings ~18.5%, muscle stable)
}
data_freshness = {k: '2026-04-24' for k in ['cardiovascular','metabolic','inflammation','liver_kidney']}
data_freshness['sleep_recovery'] = '2026-05-01'
data_freshness['body_composition'] = '2026-04-26'

# Composite weighted: 25 cardio + 20 metabolic + 20 sleep_recovery + 15 inflammation + 10 liver + 10 body
composite_score = (75*0.25 + 88*0.2 + 82*0.2 + 88*0.15 + 84*0.1 + 80*0.1)
# Convert score to bio age: linear from 51.8 chrono. Score 100 = ~10 years younger; Score 50 = 0 delta.
# Empirical: prior April composite was 45.8 with similar scores. Let's compute consistent.
# Using prior calibration: composite_age = chrono - (score - 50) * 0.24
chrono = 51.8
composite_age = round(chrono - (composite_score - 50) * 0.24, 1)
age_delta = round(composite_age - chrono, 1)

bioage_row = {
    'date': '2026-04-24',
    'whoop_age': 41.1,
    'pace_of_aging': 0.85,
    'vascular_age': 43,
    'body_fat_pct': 18.5,
    'visceral_fat_level': 4,
    'composite_bio_age': composite_age,
    'chronological_age': chrono,
    'age_delta': age_delta,
    'domain_scores': domain_scores,
    'data_freshness': data_freshness,
    'source': 'manual',
    'notes': f'Post April blood panel (bt202604). Composite score {composite_score:.1f}/100. Lipid intervention validated. Vitamin D supplementation reduced.'
}
c.table('chl_bioage_logs').upsert(bioage_row, on_conflict='date').execute()
print(f"Bioage logged: composite {composite_age} (delta {age_delta}) — score {composite_score:.1f}")

print("\n=== DONE ===")
print(f"Panel:    {PANEL_ID} (date 2026-04-24)")
print(f"Markers:  {len(marker_rows)}")
print(f"Actions:  {len(action_rows)}")
print(f"Health score: {panel['health_score']}/100  (was 72 in Jan)")
print(f"Composite bio age: {composite_age} years  (chrono {chrono}, delta {age_delta})")
