import requests
import json
import os

TOKEN_FILE = 'notion_api_key.txt'
PARENT_PAGE_ID = "2e088f42-8544-8024-8289-deb383da3818" # "Roboplanet" page

def main():
    try:
        with open(TOKEN_FILE, 'r') as f:
            token = f.read().strip()
    except FileNotFoundError:
        print(f"Error: Could not find '{TOKEN_FILE}'")
        return

    print(f"Creating 'Hello World' page under parent {PARENT_PAGE_ID}...")

    url = "https://api.notion.com/v1/pages"
    headers = {
        "Authorization": f"Bearer {token}",
        "Notion-Version": "2022-06-28",
        "Content-Type": "application/json"
    }

    payload = {
        "parent": { "page_id": PARENT_PAGE_ID },
        "properties": {
            "title": [
                {
                    "text": {
                        "content": "Hello World"
                    }
                }
            ]
        },
        "children": [
            {
                "object": "block",
                "type": "paragraph",
                "paragraph": {
                    "rich_text": [
                        {
                            "type": "text",
                            "text": {
                                "content": "This page was created automatically by the GTM Engine Bot."
                            }
                        }
                    ]
                }
            }
        ]
    }

    try:
        response = requests.post(url, headers=headers, json=payload)
        response.raise_for_status()
        
        data = response.json()
        print("\n=== SUCCESS ===")
        print(f"New page created!")
        print(f"URL: {data.get('url')}")
        
    except requests.exceptions.HTTPError as e:
        print(f"\n=== ERROR ===")
        print(f"HTTP Error: {e}")
        print(f"Response: {response.text}")
    except Exception as e:
        print(f"\n=== ERROR ===")
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    main()