# create_feature_translator_db.py
import requests
import time
import json

# --- Configuration ---
try:
    with open("notion_token.txt", "r") as f:
        NOTION_TOKEN = f.read().strip()
except FileNotFoundError:
    print("Error: notion_token.txt not found.")
    exit(1)

PARENT_PAGE_ID = "2e088f42854480248289deb383da3818" 
NOTION_VERSION = "2022-06-28"
NOTION_API_BASE_URL = "https://api.notion.com/v1"

HEADERS = {
    "Authorization": f"Bearer {NOTION_TOKEN}",
    "Notion-Version": NOTION_VERSION,
    "Content-Type": "application/json",
}

# --- Database Schema ---
DB_NAME = "Feature-to-Value Translator"
DB_SCHEMA = {
    "title": [{"type": "text", "text": {"content": DB_NAME}}],
    "properties": {
        "Feature": {"title": {}},
        "Story (Benefit)": {"rich_text": {}},
        "Headline": {"rich_text": {}},
        "Product Master": {
            "relation": {
                "database_id": "2e288f42-8544-81d8-96f5-c231f84f719a", # Product Master DB ID
                "dual_property": {}
            }
        }
    }
}

# --- Main Logic ---
def main():
    print(f"Attempting to create database: {DB_NAME}")
    create_url = f"{NOTION_API_BASE_URL}/databases"
    payload = {
        "parent": {"type": "page_id", "page_id": PARENT_PAGE_ID},
        "title": DB_SCHEMA["title"],
        "properties": DB_SCHEMA["properties"],
    }
    try:
        response = requests.post(create_url, headers=HEADERS, json=payload)
        response.raise_for_status()
        db_data = response.json()
        db_id = db_data["id"]
        print(f"Successfully created database '{DB_NAME}' with ID: {db_id}")
        print("\n--- IMPORTANT ---")
        print("Please update 'Notion_Dashboard.md' with this new ID.")
        print(f"'Feature-to-Value Translator': '{db_id}'")
        print("-------------------")

    except requests.exceptions.HTTPError as e:
        print(f"HTTP Error creating database {DB_NAME}: {e}")
        print(f"Response content: {response.text}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    main()
