import requests
import json
import os

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

def create_product_database(token):
    print(f"Creating '📦 RoboPlanet Product Master' database under parent {PARENT_PAGE_ID}...")
    
    url = "https://api.notion.com/v1/databases"
    headers = {
        "Authorization": f"Bearer {token}",
        "Notion-Version": "2022-06-28",
        "Content-Type": "application/json"
    }

    database_definition = {
        "parent": {"type": "page_id", "page_id": PARENT_PAGE_ID},
        "title": [{"type": "text", "text": {"content": "📦 RoboPlanet Product Master"}}],
        "properties": {
            "Model Name": {"title": {}},
            "Brand": {"select": {"options": [
                {"name": "VIGGO", "color": "blue"}, 
                {"name": "PUDU", "color": "orange"}
            ]}},
            "Category": {"select": {"options": [
                {"name": "cleaning", "color": "green"}, 
                {"name": "service", "color": "blue"}, 
                {"name": "security", "color": "red"}
            ]}},
            # Core Specs
            "Battery Runtime (min)": {"number": {"format": "number"}},
            "Charge Time (min)": {"number": {"format": "number"}},
            "Weight (kg)": {"number": {"format": "number"}},
            "Max Slope (deg)": {"number": {"format": "number"}},
            # Cleaning Layer
            "Fresh Water (l)": {"number": {"format": "number"}},
            "Area Performance (m2/h)": {"number": {"format": "number"}},
            # Metadata
            "Manufacturer URL": {"url": {}},
            "GTM Status": {"status": {}}
        }
    }
    
    try:
        response = requests.post(url, headers=headers, json=database_definition)
        response.raise_for_status()
        new_db = response.json()
        print(f"\n=== SUCCESS ===")
        print(f"Database created! ID: {new_db['id']}")
        print(f"URL: {new_db.get('url')}")
        return new_db['id']
    except requests.exceptions.HTTPError as e:
        print(f"\n=== ERROR ===")
        print(f"HTTP Error: {e}")
        print(f"Response: {response.text}")
        return None

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

    db_id = create_product_database(token)

if __name__ == "__main__":
    main()
