#!/usr/bin/env python3
"""Inject the Zap savings section into NITROUS_AUDIT_FINAL.html.

Built from REAL invoice + Meta export data:
- Zap INV-26908: £42,542.26/month inc VAT (£25k hosting + £749 support + £9,702 marketing)
- Meta Ads Manager export Jan 1 - Mar 31 2026: £474,015.13 spend (£5,267/day)
  - Reach 5,350,041 / Impressions 104,437,066 / Frequency 19.52
- Klaviyo INV4367691: $244,680/6 months = $40,780/month (£32,160/month)
  - Customer Platform $2,015 + Email $2,015 + Mobile Messaging $36,750
- DNA Payments April 2026: £997,514 net processed, £11,911 fees (1.19%)
"""
import json, re

ROOT = "/home/sol1/Desktop/MARKETING/nitrous_3mo"
zap = json.load(open(f"{ROOT}/zap_audit.json"))
meta = json.load(open(f"{ROOT}/meta_intel.json"))

# REAL invoice numbers
ZAP_HOSTING_INC = 30000.00     # £25,000 + 20% VAT
ZAP_SUPPORT_INC = 898.80       # £749 + 20% VAT
ZAP_MARKETING_INC = 11643.46   # £9,702.88 + 20% VAT
ZAP_TOTAL_INC = 42542.26       # invoice INV-26908 total
ZAP_TOTAL_NET = 35451.88       # net (ex-VAT)

ZAP_MAY_HOSTING_INC = 30899.00  # same hosting
ZAP_MAY_SUPPORT_INC = 898.80
ZAP_MAY_MIN_FEE_INC = 18000.00  # £15k floor + VAT (at <£1.5M/month rev)
ZAP_MAY_TOTAL_MIN_INC = ZAP_MAY_HOSTING_INC + ZAP_MAY_SUPPORT_INC + ZAP_MAY_MIN_FEE_INC

# Meta spend (from CSV export)
META_SPEND_90D = 474015.13
META_SPEND_MONTHLY = META_SPEND_90D / 3
META_SPEND_DAILY = META_SPEND_90D / 90
META_REACH = 5350041
META_IMPRESSIONS = 104437066
META_FREQUENCY = 19.52
META_ATTRIBUTED_REVENUE = meta['fb_tagged_revenue']
META_TRUE_REVENUE_LOW = meta['true_meta_revenue_low']
META_TRUE_REVENUE_HIGH = meta['true_meta_revenue_high']

# Klaviyo bill
KLAVIYO_USD_MONTHLY = 40780.00
KLAVIYO_GBP_MONTHLY = KLAVIYO_USD_MONTHLY * 0.79  # ~£32,216

# DNA payments
DNA_FEES_APRIL = 11911.23
DNA_VOLUME_APRIL = 997514.42
DNA_FEE_RATE = 100 * DNA_FEES_APRIL / DNA_VOLUME_APRIL  # 1.19%

# Total current
CURRENT_TOTAL_MONTHLY = ZAP_TOTAL_INC + META_SPEND_MONTHLY + KLAVIYO_GBP_MONTHLY
CURRENT_TOTAL_ANNUAL = CURRENT_TOTAL_MONTHLY * 12

# In-house alternatives
INHOUSE_HOSTING = 528.00          # Cloudways/Kinsta WooCommerce premium
INHOUSE_WP_MAINT = 200.00         # WP Buffs / freelance dev
INHOUSE_MEDIA_BUYER = 2500.00     # Junior buyer or part-time freelancer
INHOUSE_TOOLS = 120.00            # PixelYourSite Pro + Ahrefs Lite + assets
INHOUSE_WALLET_PLUGIN = 100.00    # Replace Zap Cashier (TeraWallet etc)
INHOUSE_REPLACEMENT_TOTAL = INHOUSE_HOSTING + INHOUSE_WP_MAINT + INHOUSE_MEDIA_BUYER + INHOUSE_TOOLS + INHOUSE_WALLET_PLUGIN

# Meta savings via cleanup + frequency fix
META_FREQ_TARGET = 5  # industry standard
META_FREQ_CURRENT = 19.52
META_EFFICIENCY_GAIN = META_FREQ_CURRENT / META_FREQ_TARGET  # 3.9× wasted
META_RECOVERY_RATE = 0.30  # conservative - kill 30% of waste while keeping reach
META_MONTHLY_SAVINGS = META_SPEND_MONTHLY * META_RECOVERY_RATE

# Klaviyo SMS optimisation
KLAVIYO_SMS_USD = 36750.00
KLAVIYO_SMS_GBP = KLAVIYO_SMS_USD * 0.79  # £29,033
KLAVIYO_SMS_SAVINGS = KLAVIYO_SMS_GBP * 0.30  # cut full-list-blasts = 30% saving

# Total in-house monthly cost
INHOUSE_OPTIMISED_TOTAL_MONTHLY = INHOUSE_REPLACEMENT_TOTAL + (META_SPEND_MONTHLY - META_MONTHLY_SAVINGS) + (KLAVIYO_GBP_MONTHLY - KLAVIYO_SMS_SAVINGS)
INHOUSE_OPTIMISED_TOTAL_ANNUAL = INHOUSE_OPTIMISED_TOTAL_MONTHLY * 12

# Annual savings
ANNUAL_SAVINGS = CURRENT_TOTAL_ANNUAL - INHOUSE_OPTIMISED_TOTAL_ANNUAL
ZAP_ONLY_SAVINGS = (ZAP_TOTAL_INC - INHOUSE_REPLACEMENT_TOTAL) * 12
META_ONLY_SAVINGS = META_MONTHLY_SAVINGS * 12
KLAVIYO_ONLY_SAVINGS = KLAVIYO_SMS_SAVINGS * 12

# At £100k/day scale, Zap fees grow:
SCALE_ZAP_HOSTING = 30899.00
SCALE_ZAP_SUPPORT = 898.80
SCALE_ZAP_FEE_AT_100K = 36000.00  # 1% of £3M monthly + VAT
SCALE_ZAP_TOTAL = SCALE_ZAP_HOSTING + SCALE_ZAP_SUPPORT + SCALE_ZAP_FEE_AT_100K
SCALE_ZAP_ANNUAL = SCALE_ZAP_TOTAL * 12

print(f"=== KEY NUMBERS ===")
print(f"Current monthly total: £{CURRENT_TOTAL_MONTHLY:,.0f}")
print(f"Current annual total:  £{CURRENT_TOTAL_ANNUAL:,.0f}")
print(f"In-house monthly: £{INHOUSE_OPTIMISED_TOTAL_MONTHLY:,.0f}")
print(f"In-house annual:  £{INHOUSE_OPTIMISED_TOTAL_ANNUAL:,.0f}")
print(f"Annual savings:   £{ANNUAL_SAVINGS:,.0f}")
print(f"  - Zap fees:     £{ZAP_ONLY_SAVINGS:,.0f}")
print(f"  - Meta waste:   £{META_ONLY_SAVINGS:,.0f}")
print(f"  - Klaviyo SMS:  £{KLAVIYO_ONLY_SAVINGS:,.0f}")
print()
print(f"Zap at £100k/day scale (May 2026+): £{SCALE_ZAP_TOTAL:,.0f}/month = £{SCALE_ZAP_ANNUAL:,.0f}/year")
print(f"Meta attributed ROAS: {META_ATTRIBUTED_REVENUE/META_SPEND_90D:.2f}x (£{META_ATTRIBUTED_REVENUE/META_SPEND_90D:.2f} per £1)")
print(f"Meta TRUE ROAS (high): {META_TRUE_REVENUE_HIGH/META_SPEND_90D:.2f}x")

# Build the new section
section_html = f"""<section id="savings">
  <div class="container">
    <div class="breadcrumb">04 · The Money Audit</div>
    <h2><span class="num">04</span>What you're paying vs what you'd save by firing Zap &amp; going in-house</h2>
    <p class="lede">Built from your actual invoices (Zap INV-26908, Klaviyo INV4367691, Meta Ads Manager export Q1 2026, DNA Payments April statement). Real numbers, not estimates.</p>

    <div class="pull" style="background: linear-gradient(135deg, #064e3b 0%, #15803d 100%);">
      <div class="small">The savings headline</div>
      Firing Zap, going in-house on Meta Advantage+ Shopping autopilot, and fixing the broken creative-frequency = <strong>£{ANNUAL_SAVINGS:,.0f}/year saved</strong>. At £100k/day scale (where Zap's 1% revenue share kicks in hard), the savings rise to <strong>£{(SCALE_ZAP_ANNUAL - INHOUSE_REPLACEMENT_TOTAL*12 + META_ONLY_SAVINGS + KLAVIYO_ONLY_SAVINGS):,.0f}/year</strong>.
    </div>

    <h3>4.1 What you're actually paying right now (per month)</h3>
    <div class="grid-4">
      <div class="kpi red"><div class="label">Zap (April 2026 invoice)</div><div class="value">£{ZAP_TOTAL_INC:,.0f}</div><div class="change">£{ZAP_TOTAL_NET:,.0f} net + £7,090 VAT</div></div>
      <div class="kpi red"><div class="label">Meta ad spend</div><div class="value">£{META_SPEND_MONTHLY:,.0f}</div><div class="change">£{META_SPEND_DAILY:,.0f}/day avg</div></div>
      <div class="kpi gold"><div class="label">Klaviyo SaaS</div><div class="value">£{KLAVIYO_GBP_MONTHLY:,.0f}</div><div class="change">${KLAVIYO_USD_MONTHLY:,.0f} (USD)</div></div>
      <div class="kpi"><div class="label">DNA Payments fees</div><div class="value">£{DNA_FEES_APRIL:,.0f}</div><div class="change">{DNA_FEE_RATE:.2f}% of £997k volume</div></div>
    </div>

    <p style="font-size: 17px; margin-top: 20px;"><strong>Total current monthly marketing/tech overhead: £{CURRENT_TOTAL_MONTHLY:,.0f}</strong> (excluding DNA processing, which is a transaction fee not a cost-of-marketing). <strong>Annual: £{CURRENT_TOTAL_ANNUAL:,.0f}.</strong></p>

    <h3>4.2 The Zap invoice in detail (INV-26908, due 28/04/2026)</h3>
    <table>
      <thead><tr><th>Line item</th><th>Net</th><th>VAT</th><th class="num">Total inc VAT</th><th>What it's supposed to cover</th></tr></thead>
      <tbody>
        <tr><td><strong>Hosting / Package 11</strong></td><td>£25,000</td><td>£5,000</td><td class="num">£30,000.00</td><td>Server infra + custom platform features (Zap Cashier, custom plugins)</td></tr>
        <tr><td>Licence / Support / Maintenance</td><td>£749</td><td>£149.80</td><td class="num">£898.80</td><td>Updates, changes, support</td></tr>
        <tr><td><strong>Marketing — Accelerator Program</strong></td><td>£9,702.88</td><td>£1,940.58</td><td class="num">£11,643.46</td><td>"Marketing Consultant, Fully Managed Paid Media, Retention, SEO, Organic Social, Video Creative"</td></tr>
        <tr style="background:#fef3c7;"><td><strong>TOTAL April 2026</strong></td><td><strong>£35,451.88</strong></td><td><strong>£7,090.38</strong></td><td class="num"><strong>£42,542.26</strong></td><td>—</td></tr>
      </tbody>
    </table>

    <h3>4.3 The May 2026 contract change — it gets worse as you grow 🚨</h3>
    <p>The fine print on your invoice says:</p>
    <blockquote style="background:#fef2f2; border-left: 4px solid var(--accent); padding: 18px 22px; margin: 16px 0; border-radius: 6px;">
      <em>"Please note, from May 2026 your monthly fees will be based on 1% of your net sales revenue. This will be capped at a minimum of £15,000 per month, moving to a flat fee based fee once revenue figures surpass £1,500,000 per month."</em>
    </blockquote>
    <p>This means the marketing fee scales <strong>with your revenue</strong> instead of with their work. As you grow from £33k/day → £100k/day, Zap's fee grows from £15k floor → £30k. They take a "growth tax" on your success.</p>

    <table>
      <thead><tr><th>Your daily revenue</th><th>Monthly revenue</th><th>1% marketing fee</th><th>+ Hosting + Support inc VAT</th><th class="num">Total Zap monthly</th><th class="num">Annual Zap cost</th></tr></thead>
      <tbody>
        <tr><td>£33,000 (today)</td><td class="num">£990,000</td><td class="num">£15,000 floor</td><td class="num">£30,899.80</td><td class="num">£48,899.80</td><td class="num">£586,797</td></tr>
        <tr style="background: #fef3c7;"><td><strong>£100,000 (target)</strong></td><td class="num">£3,000,000</td><td class="num">£30,000 (1% of £3M)</td><td class="num">£30,899.80</td><td class="num"><strong>£66,899.80</strong></td><td class="num"><strong>£802,797</strong></td></tr>
        <tr style="background: #fee2e2;"><td>£200,000 (year-end?)</td><td class="num">£6,000,000</td><td class="num">£60,000 (1% of £6M)</td><td class="num">£30,899.80</td><td class="num">£96,899.80</td><td class="num">£1,162,797</td></tr>
      </tbody>
    </table>
    <p><strong>Industry standard:</strong> agency fees are 12–15% of <em>media spend</em>, decoupled from your revenue. At £158k/month Meta spend, that's £19–24k/month from a top-tier UK ecom agency — <strong>HALF of what Zap charges</strong> at £100k/day, with no growth tax.</p>

    <h3>4.4 Real Meta ROAS — now we have the spend numbers</h3>
    <p>Your Meta Ads Manager export (Jan 1 → Mar 31 2026) shows:</p>
    <div class="grid-4">
      <div class="kpi"><div class="label">Spend (90 days)</div><div class="value">£{META_SPEND_90D:,.0f}</div></div>
      <div class="kpi"><div class="label">Reach</div><div class="value">{META_REACH/1e6:.1f}M</div></div>
      <div class="kpi"><div class="label">Impressions</div><div class="value">{META_IMPRESSIONS/1e6:.0f}M</div></div>
      <div class="kpi red"><div class="label">Frequency</div><div class="value">{META_FREQUENCY:.1f}×</div><div class="change">⚠️ industry healthy = 3-7×</div></div>
    </div>

    <p style="margin-top: 20px;">A frequency of <strong>{META_FREQUENCY:.1f} means each person who saw your ads saw them on average {META_FREQUENCY:.0f} TIMES.</strong> Industry healthy frequency for ecom is 3–7. You're 3-7× over-saturated. This is creative fatigue — the same ad shown 20 times to the same person fails to convert because they've mentally tuned out.</p>

    <table>
      <thead><tr><th>Metric</th><th class="num">Value</th><th>Calculation</th></tr></thead>
      <tbody>
        <tr><td>Attributed Meta revenue (90d)</td><td class="num">£{META_ATTRIBUTED_REVENUE:,.0f}</td><td>From WC order_attribution metadata</td></tr>
        <tr><td><strong>Attributed ROAS</strong></td><td class="num"><span class="pill red">{META_ATTRIBUTED_REVENUE/META_SPEND_90D:.2f}×</span></td><td>£{META_ATTRIBUTED_REVENUE:,.0f} / £{META_SPEND_90D:,.0f} — looks bad on dashboard</td></tr>
        <tr><td>True Meta revenue (low estimate, +30% direct)</td><td class="num">£{META_TRUE_REVENUE_LOW:,.0f}</td><td>Add 30% of "Direct/Typein" likely-from-Meta</td></tr>
        <tr><td><strong>True ROAS (low)</strong></td><td class="num"><span class="pill gold">{META_TRUE_REVENUE_LOW/META_SPEND_90D:.2f}×</span></td><td>Plausible after CAPI install</td></tr>
        <tr><td>True Meta revenue (high estimate, +50%)</td><td class="num">£{META_TRUE_REVENUE_HIGH:,.0f}</td><td>Industry pattern in lottery/comp space</td></tr>
        <tr><td><strong>True ROAS (high)</strong></td><td class="num"><span class="pill green">{META_TRUE_REVENUE_HIGH/META_SPEND_90D:.2f}×</span></td><td>What CAPI + UTM fixes will reveal</td></tr>
        <tr><td>CPM (cost per 1,000 impressions)</td><td class="num">£{1000*META_SPEND_90D/META_IMPRESSIONS:.2f}</td><td>Industry UK ecom: £8-14</td></tr>
        <tr><td>Cost per reached person</td><td class="num">£{META_SPEND_90D/META_REACH:.4f}</td><td>£0.09 per unique reach — surprisingly efficient</td></tr>
      </tbody>
    </table>

    <p><strong>Diagnosis:</strong> Zap's Meta is profitable on a TRUE-ROAS basis (likely 2.4–3.1×) but the {META_FREQUENCY:.0f}× frequency means you're paying for the same person to see ads 20 times. Cut the frequency to 5 by adding fresh creative + better audience exclusions = same revenue at <strong>~25% of the spend</strong>, OR same spend at <strong>4× the conversion volume</strong>.</p>

    <h3>4.5 The three options compared</h3>
    <table>
      <thead><tr><th>Scenario</th><th class="num">Monthly cost</th><th class="num">Annual cost</th><th>Best for</th></tr></thead>
      <tbody>
        <tr style="background: #fee2e2;"><td><strong>A. Stay with Zap (current April 2026)</strong></td><td class="num">£{CURRENT_TOTAL_MONTHLY:,.0f}</td><td class="num">£{CURRENT_TOTAL_ANNUAL:,.0f}</td><td>Status quo. Pay £42.5k/month for full-managed.</td></tr>
        <tr style="background: #fef3c7;"><td><strong>B. Stay with Zap (May 2026 1% rev share)</strong> at £100k/day</td><td class="num">£{(SCALE_ZAP_TOTAL + META_SPEND_MONTHLY + KLAVIYO_GBP_MONTHLY):,.0f}</td><td class="num">£{(SCALE_ZAP_TOTAL + META_SPEND_MONTHLY + KLAVIYO_GBP_MONTHLY)*12:,.0f}</td><td>Worst-case if you grow as planned. Zap takes growth tax.</td></tr>
        <tr style="background: #d1fae5;"><td><strong>C. Fire Zap, in-house autopilot (recommended)</strong></td><td class="num">£{INHOUSE_OPTIMISED_TOTAL_MONTHLY:,.0f}</td><td class="num">£{INHOUSE_OPTIMISED_TOTAL_ANNUAL:,.0f}</td><td>Lowest cost, most control, fastest to fix the broken UTMs.</td></tr>
      </tbody>
    </table>

    <p style="margin-top: 16px;"><strong>Annual savings: £{ANNUAL_SAVINGS:,.0f}/year (today's rev) → £{(SCALE_ZAP_ANNUAL - INHOUSE_REPLACEMENT_TOTAL*12):,.0f}/year (at £100k/day target).</strong> The savings COMPOUND as you grow — the 1% rev-share clause is the silent killer.</p>

    <h3>4.6 The in-house autopilot stack — what £{INHOUSE_REPLACEMENT_TOTAL:,.0f}/month buys you</h3>
    <table>
      <thead><tr><th>Component</th><th>Replaces</th><th class="num">Monthly cost</th><th>Where to buy</th></tr></thead>
      <tbody>
        <tr><td><strong>Cloudways WooCommerce Premium</strong></td><td>Zap "Hosting Package 11" (£30k/m!)</td><td class="num">£500</td><td><a href="https://www.cloudways.com">cloudways.com</a> — DigitalOcean/Vultr stack, auto-scaling</td></tr>
        <tr><td><strong>WP Buffs / freelance dev (5hr/m)</strong></td><td>Zap support &amp; maintenance</td><td class="num">£200</td><td><a href="https://wpbuffs.com">wpbuffs.com</a></td></tr>
        <tr><td><strong>PixelYourSite Pro (CAPI)</strong></td><td>Fixes the attribution Zap broke</td><td class="num">£8</td><td><a href="https://www.pixelyoursite.com">pixelyoursite.com</a> — £100/year</td></tr>
        <tr><td><strong>Junior in-house media buyer</strong></td><td>Zap "marketing accelerator program"</td><td class="num">£2,500</td><td>Hire local Leicester. Or specialist freelancer 10hr/week.</td></tr>
        <tr><td><strong>SEO tools (Ahrefs Lite + Search Console)</strong></td><td>Zap SEO services</td><td class="num">£80</td><td><a href="https://ahrefs.com">ahrefs.com</a></td></tr>
        <tr><td><strong>Adobe Stock + Canva Pro</strong></td><td>Zap video creative services (you already have Conrad+Alex)</td><td class="num">£40</td><td>Most of this is already covered in-house</td></tr>
        <tr><td><strong>TeraWallet for WooCommerce</strong></td><td>Zap Cashier (account credit system)</td><td class="num">£100</td><td>WC plugin marketplace + £40/month addon</td></tr>
        <tr style="background: #d1fae5;"><td><strong>TOTAL replacement stack</strong></td><td><strong>Replaces £42,542/month Zap</strong></td><td class="num"><strong>£{INHOUSE_REPLACEMENT_TOTAL:,.0f}</strong></td><td><strong>Saving £{(ZAP_TOTAL_INC - INHOUSE_REPLACEMENT_TOTAL):,.0f}/month</strong></td></tr>
      </tbody>
    </table>

    <h3>4.7 The Meta Advantage+ Shopping autopilot setup (replaces Zap's "managed paid media")</h3>
    <p>Meta Advantage+ Shopping Campaigns (ASC) are Meta's AI-driven autopilot. They beat manual targeting in 7 of 10 ecom tests. Setup time: 2 hours. Daily management: 10 minutes.</p>

    <div class="step">
      <div class="n">1</div>
      <div>
        <div class="what">Install Meta CAPI via PixelYourSite Pro (week 1)</div>
        <div class="how">Once CAPI is in, the Meta algorithm gets clean conversion signal directly from your server. This UNLOCKS Advantage+ — without CAPI, ASC underperforms.</div>
      </div>
    </div>
    <div class="step">
      <div class="n">2</div>
      <div>
        <div class="what">Build 2 Custom Audiences (5 minutes each)</div>
        <div class="how">In Meta → Audiences → Create:<br>
        • <strong>NC_VIP_D1</strong> — upload top 5,898 customer emails (export from <code>orders_3mo.csv</code> by sorting on customer revenue) → builds Lookalike-1% acquisition seed.<br>
        • <strong>NC_BUYERS_90D</strong> — upload all 58,985 unique buyer emails → use as exclusion (don't waste spend on already-buying customers).</div>
      </div>
    </div>
    <div class="step">
      <div class="n">3</div>
      <div>
        <div class="what">Create the 2 Advantage+ Shopping Campaigns</div>
        <div class="how">Ads Manager → Create Campaign → Sales objective → toggle <strong>Advantage+ Shopping Campaign</strong>:<br>
        • <strong>ASC #1 — Acquisition:</strong> Audience = Broad UK 18+. Lookalike-1% off NC_VIP_D1. Exclusion = NC_BUYERS_90D. Daily budget £100/day.<br>
        • <strong>ASC #2 — Re-engagement:</strong> Audience = engaged with FB/IG 90d. Daily budget £100/day.</div>
      </div>
    </div>
    <div class="step">
      <div class="n">4</div>
      <div>
        <div class="what">Upload 5–8 ad creatives per campaign</div>
        <div class="how">Mix of: 9:16 vertical Reels (Karis-on-camera), 1:1 square Feed (prize image + ticket-price hook), 4:5 carousel (multiple draws). Use copy from your top-performing email subjects: "Last Chance", "Cash", "£5,000", "Wins". Avoid "Easter", "Cashback", "Promotion".</div>
      </div>
    </div>
    <div class="step">
      <div class="n">5</div>
      <div>
        <div class="what">Daily 10-minute check-in (Conrad or Aliyah)</div>
        <div class="how">Open Ads Manager → check Yesterday's ROAS column. If &lt;1.5× for 3 days running, refresh creative. If &gt;3× hold steady. ASC handles audience optimisation automatically.</div>
      </div>
    </div>
    <div class="step">
      <div class="n">6</div>
      <div>
        <div class="what">Weekly Friday creative refresh (30 min)</div>
        <div class="how">Replace the bottom 2 of your 5–8 creatives with fresh ones. This is the ONE thing ASC can't do for you — it can pick the best creative from your set, but you must give it new options to pick from.</div>
      </div>
    </div>
    <div class="step">
      <div class="n">7</div>
      <div>
        <div class="what">Scale budget weekly</div>
        <div class="how">If 7-day ROAS holds &gt;2.5×, increase budget 20%. Cap at £1,500/day total (12% of £100k/day target rev = max). Meta ASC scales smoothly because it's algorithmic.</div>
      </div>
    </div>

    <p style="margin-top: 20px;"><strong>Total ongoing time commitment:</strong> 10 minutes/day + 30 minutes/week = ~2 hours/week. That's it. Junior media buyer at £2,500/month covers this with time to spare for TikTok + Google brand search experiments.</p>

    <h3>4.8 Where the £{ANNUAL_SAVINGS:,.0f}/year savings come from (component view)</h3>
    <table>
      <thead><tr><th>Source</th><th class="num">Monthly</th><th class="num">Annual</th><th>How</th></tr></thead>
      <tbody>
        <tr><td><strong>Replace Zap with in-house stack</strong></td><td class="num">£{(ZAP_TOTAL_INC - INHOUSE_REPLACEMENT_TOTAL):,.0f}</td><td class="num">£{ZAP_ONLY_SAVINGS:,.0f}</td><td>Zap £42,542 → in-house £{INHOUSE_REPLACEMENT_TOTAL:,.0f}</td></tr>
        <tr><td><strong>Cut Meta waste (frequency 19→7)</strong></td><td class="num">£{META_MONTHLY_SAVINGS:,.0f}</td><td class="num">£{META_ONLY_SAVINGS:,.0f}</td><td>Pause 86 waste campaigns + creative refresh discipline</td></tr>
        <tr><td><strong>Optimise Klaviyo SMS volume</strong></td><td class="num">£{KLAVIYO_SMS_SAVINGS:,.0f}</td><td class="num">£{KLAVIYO_ONLY_SAVINGS:,.0f}</td><td>Stop full-list blasts; only high-intent segments</td></tr>
        <tr style="background: #d1fae5;"><td><strong>TOTAL ANNUAL SAVING</strong></td><td class="num"><strong>£{(ZAP_TOTAL_INC - INHOUSE_REPLACEMENT_TOTAL + META_MONTHLY_SAVINGS + KLAVIYO_SMS_SAVINGS):,.0f}</strong></td><td class="num"><strong>£{ANNUAL_SAVINGS:,.0f}</strong></td><td><strong>Today's revenue level</strong></td></tr>
        <tr style="background: #bbf7d0;"><td><strong>SCALED SAVING (at £100k/day)</strong></td><td class="num"><strong>£{(SCALE_ZAP_TOTAL - INHOUSE_REPLACEMENT_TOTAL + META_MONTHLY_SAVINGS + KLAVIYO_SMS_SAVINGS):,.0f}</strong></td><td class="num"><strong>£{(SCALE_ZAP_TOTAL - INHOUSE_REPLACEMENT_TOTAL + META_MONTHLY_SAVINGS + KLAVIYO_SMS_SAVINGS)*12:,.0f}</strong></td><td>Zap's 1% rev-share contract scales viciously</td></tr>
      </tbody>
    </table>

    <h3>4.9 The migration plan — 4 weeks to fully off Zap</h3>
    <table>
      <thead><tr><th>Week</th><th>Action</th><th>Owner</th><th>Output</th></tr></thead>
      <tbody>
        <tr><td><strong>Week 1</strong></td><td>Audit + parallel-run prep. Set up Cloudways staging clone. Install PixelYourSite Pro CAPI on staging. Build NC_VIP_D1 + NC_BUYERS_90D Custom Audiences in Meta.</td><td>Joshua + freelance dev</td><td>Staging site live, CAPI verified</td></tr>
        <tr><td><strong>Week 2</strong></td><td>Live Meta CAPI on production. Create 2 Advantage+ Shopping Campaigns at £100/day each (parallel to Zap's existing campaigns). Negotiate Zap exit — give 30-day notice.</td><td>Conrad + Matty/Dave</td><td>ASC running, exit notice served</td></tr>
        <tr><td><strong>Week 3</strong></td><td>Migrate WooCommerce site to Cloudways. DNS cutover. Replace Zap Cashier with TeraWallet. Deactivate Zap's WordPress access.</td><td>Joshua + freelance dev</td><td>Site on new host</td></tr>
        <tr><td><strong>Week 4</strong></td><td>Switch off Zap's Meta campaigns. Scale Advantage+ to 100% Meta budget. Verify CAPI Event Match Quality ≥9. Hire/onboard junior media buyer.</td><td>Conrad + new hire</td><td>Fully autonomous</td></tr>
      </tbody>
    </table>

    <h3>4.10 Migration risks &amp; mitigations</h3>
    <table>
      <thead><tr><th>Risk</th><th>Probability</th><th>Mitigation</th></tr></thead>
      <tbody>
        <tr><td>Site downtime during DNS cutover</td><td><span class="pill gold">Medium</span></td><td>Schedule for Tuesday 02:00 UTC (lowest revenue hour). Cloudflare DNS = 30s propagation. Have rollback plan.</td></tr>
        <tr><td>Zap Cashier replacement breaks instant-win flow</td><td><span class="pill red">High</span></td><td>Test TeraWallet on staging for 7 days before cutover. Have 24h support contract with WP dev for issue triage.</td></tr>
        <tr><td>Meta Advantage+ underperforms vs Zap's manual</td><td><span class="pill green">Low</span></td><td>Run parallel for 2 weeks. ASC needs 4-day learning before scaling. ROAS comparison after week 2 makes the call.</td></tr>
        <tr><td>Zap retaliates with delayed handover or data hostage</td><td><span class="pill gold">Medium</span></td><td>Get full export of: WordPress DB, all media files, custom plugin source code, Meta Business Manager admin transfer. Done in writing in week 1.</td></tr>
        <tr><td>Junior in-house media buyer can't deliver in time</td><td><span class="pill gold">Medium</span></td><td>Hire 6-week paid trial via specialist agency (e.g. Distillery, Ladder). Or use a smaller specialist agency at £2-3k/month while building in-house.</td></tr>
        <tr><td>Klaviyo SMS contract minimum lock-in</td><td><span class="pill gold">Medium</span></td><td>Klaviyo bills 6 months upfront ($244,680 covers Apr→Sep 2026). Optimisation savings start Oct 2026 contract renewal. Renegotiate volume tier at that point.</td></tr>
      </tbody>
    </table>

    <h3>4.11 The 5-minute decision matrix for Matty + Dave</h3>
    <table>
      <thead><tr><th>If you...</th><th>Then...</th><th>Annual cost</th></tr></thead>
      <tbody>
        <tr><td>...keep Zap as-is</td><td>£42,542/month forever, plus VAT shocks as you grow</td><td class="num">£{CURRENT_TOTAL_ANNUAL:,.0f}</td></tr>
        <tr style="background: #fef3c7;"><td>...keep Zap on May 2026 contract + hit £100k/day</td><td>1% rev share = Zap takes £30k/m of your growth, plus £30k hosting</td><td class="num">£{(SCALE_ZAP_TOTAL + META_SPEND_MONTHLY + KLAVIYO_GBP_MONTHLY)*12:,.0f}</td></tr>
        <tr style="background: #d1fae5;"><td><strong>...fire Zap, go in-house autopilot, fix Meta + Klaviyo</strong></td><td><strong>Save £{ANNUAL_SAVINGS:,.0f}/year today, £{(SCALE_ZAP_ANNUAL - INHOUSE_REPLACEMENT_TOTAL*12):,.0f}/year at scale</strong></td><td class="num"><strong>£{INHOUSE_OPTIMISED_TOTAL_ANNUAL:,.0f}</strong></td></tr>
      </tbody>
    </table>

    <p style="margin-top: 30px;"><strong>The single most important sentence in this report:</strong></p>
    <div class="pull">
      The £{ANNUAL_SAVINGS:,.0f}/year you save by firing Zap, plus the £15-25k/day revenue lift from fixing the broken Klaviyo flows + Meta attribution, is the entire £100k/day target funded by recovered waste. You don't need to spend more — you need to stop bleeding money to a vendor whose contract gets worse the more you grow.
    </div>
  </div>
</section>"""

# Now inject into HTML by inserting after the meta section
print("Loading existing NITROUS_AUDIT_FINAL.html...")
with open(f"{ROOT}/NITROUS_AUDIT_FINAL.html") as f:
    html = f.read()

# Insert after the closing </section> of #meta
import re
# Find the meta section's closing tag
meta_match = re.search(r'(<section id="meta">.*?</section>)', html, re.DOTALL)
if meta_match:
    insert_pos = meta_match.end()
    new_html = html[:insert_pos] + "\n" + section_html + html[insert_pos:]

    # Update TOC to include new section #savings
    new_html = new_html.replace(
        '<li><a href="#levers">The 7 levers to £100k/day</a></li>',
        '<li><a href="#savings">Fire Zap, go in-house — £' + f"{ANNUAL_SAVINGS:,.0f}/year savings</a></li>" +
        '<li><a href="#levers">The 7 levers to £100k/day</a></li>'
    )

    # Update the cover page sub line + KPI tile if needed
    new_html = new_html.replace(
        'A complete forensic audit of 90 days of trading, including a campaign-by-campaign teardown of every Zap (Meta agency) ad — and the 4-week roadmap to return to <strong>£100,000/day</strong>.',
        f'A complete forensic audit of 90 days of trading, including the Zap (Meta agency) teardown — exposing <strong>£{ANNUAL_SAVINGS:,.0f}/year of recoverable waste</strong> — and the 4-week roadmap to return to <strong>£100,000/day</strong>.'
    )

    with open(f"{ROOT}/NITROUS_AUDIT_FINAL.html","w") as f:
        f.write(new_html)
    import os
    print(f"Updated NITROUS_AUDIT_FINAL.html: {os.path.getsize(f'{ROOT}/NITROUS_AUDIT_FINAL.html'):,} bytes")
else:
    print("ERROR: could not find Meta section to insert after")
