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_ID = "2ec88f4285448014ab38ea664b4c2b81"

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

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

def list_pages_and_keys():
    url = f"https://api.notion.com/v1/databases/{NOTION_DB_ID}/query"
    payload = {
        "page_size": 10 # Just list a few to see structure
    }
    
    response = requests.post(url, headers=headers, json=payload)
    
    if response.status_code != 200:
        print(f"Error fetching data: {response.status_code} - {response.text}")
        return

    results = response.json().get("results", [])
    
    if not results:
        print("No pages found.")
        return

    print(f"Found {len(results)} pages.")
    
    # Print keys from the first page
    first_page = results[0]
    props = first_page["properties"]
    print("\n--- Property Keys Found ---")
    for key in props.keys():
        print(f"- {key}")

    print("\n--- Page Titles (Verticals) ---")
    for page in results:
        title_prop = page["properties"].get("Vertical", {}).get("title", []) # Assuming title prop is named "Vertical" based on user input
        if not title_prop:
             # Try finding the title property dynamically if "Vertical" is wrong
             for k, v in page["properties"].items():
                 if v["id"] == "title":
                     title_prop = v["title"]
                     break
        
        if title_prop:
            title = title_prop[0]["plain_text"]
            print(f"- {title}")
        else:
            print("- (No Title)")

if __name__ == "__main__":
    list_pages_and_keys()
