import os
import requests
import json
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

NOTION_API_KEY = os.getenv("NOTION_API_KEY")
NOTION_DB_VERTICALS = "2ec88f4285448014ab38ea664b4c2b81"
NOTION_DB_PRODUCTS = "2ec88f42854480f0b154f7a07342eb58"

if not NOTION_API_KEY:
    print("Error: NOTION_API_KEY not found.")
    exit(1)

headers = {
    "Authorization": f"Bearer {NOTION_API_KEY}",
    "Notion-Version": "2022-06-28",
    "Content-Type": "application/json"
}

def fetch_all_pages(db_id):
    pages = []
    has_more = True
    start_cursor = None

    while has_more:
        url = f"https://api.notion.com/v1/databases/{db_id}/query"
        payload = {"page_size": 100}
        if start_cursor:
            payload["start_cursor"] = start_cursor
        
        response = requests.post(url, headers=headers, json=payload)
        if response.status_code != 200:
            print(f"Error fetching DB {db_id}: {response.status_code} - {response.text}")
            break
        
        data = response.json()
        pages.extend(data.get("results", []))
        has_more = data.get("has_more", False)
        start_cursor = data.get("next_cursor")
        
    return pages

def get_property_text(page, prop_name):
    props = page.get("properties", {})
    prop = props.get(prop_name)
    if not prop:
        return ""
    
    prop_type = prop.get("type")
    
    if prop_type == "title":
        return "".join([t["plain_text"] for t in prop.get("title", [])])
    elif prop_type == "rich_text":
        return "".join([t["plain_text"] for t in prop.get("rich_text", [])])
    elif prop_type == "select":
        select = prop.get("select")
        return select.get("name") if select else ""
    elif prop_type == "multi_select":
        return ", ".join([s["name"] for s in prop.get("multi_select", [])])
    elif prop_type == "relation":
        return [r["id"] for r in prop.get("relation", [])]
    else:
        return f"[Type: {prop_type}]"

def main():
    print("--- 1. Fetching Product Categories ---")
    product_pages = fetch_all_pages(NOTION_DB_PRODUCTS)
    product_map = {}
    for p in product_pages:
        p_id = p["id"]
        # Product Category name is likely the title property
        # Let's find the title property key dynamically
        title_key = next((k for k, v in p["properties"].items() if v["id"] == "title"), "Name")
        name = get_property_text(p, title_key)
        product_map[p_id] = name
        # print(f"Product: {name} ({p_id})")

    print(f"Loaded {len(product_map)} products.")

    print("\n--- 2. Fetching Verticals ---")
    vertical_pages = fetch_all_pages(NOTION_DB_VERTICALS)
    
    print("\n--- 3. Analysis ---")
    for v in vertical_pages:
        # Determine Title Key (Vertical Name)
        title_key = next((k for k, v in v["properties"].items() if v["id"] == "title"), "Vertical")
        vertical_name = get_property_text(v, title_key)
        
        # Primary Product
        pp_ids = get_property_text(v, "Primary Product Category")
        pp_names = [product_map.get(pid, f"Unknown ({pid})") for pid in pp_ids] if isinstance(pp_ids, list) else []
        
        # Secondary Product
        sp_ids = get_property_text(v, "Secondary Product")
        sp_names = [product_map.get(pid, f"Unknown ({pid})") for pid in sp_ids] if isinstance(sp_ids, list) else []
        
        # Pains & Gains
        pains = get_property_text(v, "Pains")
        gains = get_property_text(v, "Gains")

        print(f"\n### {vertical_name}")
        print(f"**Primary Product:** {', '.join(pp_names)}")
        print(f"**Secondary Product:** {', '.join(sp_names)}")
        print(f"**Pains:**\n{pains.strip()}")
        print(f"**Gains:**\n{gains.strip()}")
        print("-" * 40)

if __name__ == "__main__":
    main()
