#!/usr/bin/env python3
"""Add clickable source links to Phat Foods Ch1 Notion page."""

import os
import requests

API_KEY = open(os.path.expanduser("~/.config/notion/api_key")).read().strip()
HEADERS = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json",
    "Notion-Version": "2022-06-28"
}

PAGE_ID = "2f8330c2-8646-81f5-87ce-caa7e4a94479"

sources = [
    {"name": "GFI Plant-Based Retail Market Overview 2024", "url": "https://gfi.org/marketresearch/"},
    {"name": "SPINS Webinar - Plant-Based State of the Industry (Dec 2025)", "url": "https://www.dairyreporter.com/Article/2025/12/18/is-plant-based-dead-or-evolving/"},
    {"name": "Mintel US Plant-Based Proteins Report 2023", "url": "https://store.mintel.com/report/us-plant-based-proteins-market-report"},
    {"name": "AHDB UK - Meat and dairy reign supreme (Mar 2025)", "url": "https://ahdb.org.uk/news/meat-and-dairy-reign-supreme-in-january-as-plant-based-alternatives-see-decline"},
    {"name": "Dairy Foods - 2025 State of the Industry", "url": "https://www.dairyfoods.com/articles/98636-2025-state-of-the-dairy-industry"},
    {"name": "NIH - Sensory Properties of Plant-Based Cheese (2023)", "url": "https://pmc.ncbi.nlm.nih.gov/articles/PMC10137571/"},
    {"name": "NIH - Consumer Perception of Plant-Based Cheese (2025)", "url": "https://pmc.ncbi.nlm.nih.gov/articles/PMC12484713/"},
    {"name": "IFIC Food & Health Survey 2024", "url": "https://foodinsight.org/2024-food-health-survey/"},
    {"name": "FoodNavigator - Plant-based milk market slows (Jul 2025)", "url": "https://www.foodnavigator.com/Article/2025/07/07/plant-based-milk-market-faces-slowdown-as-dairy-makes-a-comeback/"},
    {"name": "PBFA Migration Analysis 2024", "url": "https://members.plantbasedfoods.org/checkout/pbfa-i-migration-analysis-year-4-oct-2024"},
]

blocks = [
    {"type": "divider", "divider": {}},
    {"type": "heading_1", "heading_1": {"rich_text": [{"type": "text", "text": {"content": "📚 Sources (Clickable)"}}]}},
    {"type": "paragraph", "paragraph": {"rich_text": [{"type": "text", "text": {"content": "All data points in this chapter are sourced from the following:"}}]}},
]

# Add each source as a bookmark or linked bullet
for src in sources:
    blocks.append({
        "type": "bulleted_list_item",
        "bulleted_list_item": {
            "rich_text": [
                {
                    "type": "text",
                    "text": {"content": src["name"], "link": {"url": src["url"]}}
                }
            ]
        }
    })

# Also add source mapping for each slide
blocks.extend([
    {"type": "divider", "divider": {}},
    {"type": "heading_2", "heading_2": {"rich_text": [{"type": "text", "text": {"content": "Source Mapping by Slide"}}]}},
    {"type": "bulleted_list_item", "bulleted_list_item": {"rich_text": [{"type": "text", "text": {"content": "Slide 2 (15% vs 1.6%, retreat stats): "}, "annotations": {"bold": True}}, {"type": "text", "text": {"content": "GFI Market Research 2024", "link": {"url": "https://gfi.org/marketresearch/"}}}]}},
    {"type": "bulleted_list_item", "bulleted_list_item": {"rich_text": [{"type": "text", "text": {"content": "Slide 3 ($8.1B market, lapsed buyers): "}, "annotations": {"bold": True}}, {"type": "text", "text": {"content": "Mintel 2023", "link": {"url": "https://store.mintel.com/report/us-plant-based-proteins-market-report"}}}, {"type": "text", "text": {"content": ", "}}, {"type": "text", "text": {"content": "AHDB 2025", "link": {"url": "https://ahdb.org.uk/news/meat-and-dairy-reign-supreme-in-january-as-plant-based-alternatives-see-decline"}}}]}},
    {"type": "bulleted_list_item", "bulleted_list_item": {"rich_text": [{"type": "text", "text": {"content": "Slide 4 (73% want better cheese, failure modes): "}, "annotations": {"bold": True}}, {"type": "text", "text": {"content": "NIH Sensory Study 2023", "link": {"url": "https://pmc.ncbi.nlm.nih.gov/articles/PMC10137571/"}}}, {"type": "text", "text": {"content": ", "}}, {"type": "text", "text": {"content": "NIH Consumer Study 2025", "link": {"url": "https://pmc.ncbi.nlm.nih.gov/articles/PMC12484713/"}}}]}},
    {"type": "bulleted_list_item", "bulleted_list_item": {"rich_text": [{"type": "text", "text": {"content": "Slide 5 (price premium, consumer behavior): "}, "annotations": {"bold": True}}, {"type": "text", "text": {"content": "GFI 2024", "link": {"url": "https://gfi.org/marketresearch/"}}}, {"type": "text", "text": {"content": ", "}}, {"type": "text", "text": {"content": "IFIC Survey 2024", "link": {"url": "https://foodinsight.org/2024-food-health-survey/"}}}]}},
    {"type": "bulleted_list_item", "bulleted_list_item": {"rich_text": [{"type": "text", "text": {"content": "Slide 6 (400+ fatty acids, SCFAs): "}, "annotations": {"bold": True}}, {"type": "text", "text": {"content": "NIH research on milk fat composition (primary literature)"}}]}},
    {"type": "bulleted_list_item", "bulleted_list_item": {"rich_text": [{"type": "text", "text": {"content": "Slide 8 (competitor analysis): "}, "annotations": {"bold": True}}, {"type": "text", "text": {"content": "Company websites and technology documentation"}}]}},
])

resp = requests.patch(
    f"https://api.notion.com/v1/blocks/{PAGE_ID}/children",
    headers=HEADERS,
    json={"children": blocks}
)

if resp.status_code == 200:
    print(f"✓ Added {len(blocks)} blocks with clickable sources")
else:
    print(f"Error: {resp.status_code} - {resp.text[:300]}")
