import os
import requests
import json
from dotenv import load_dotenv

load_dotenv(dotenv_path="/home/node/clawd/.env")

NOTION_TOKEN = os.getenv("NOTION_API_KEY")
HEADERS = {
    "Authorization": f"Bearer {NOTION_TOKEN}",
    "Content-Type": "application/json",
    "Notion-Version": "2022-06-28"
}

def find_db_id(query_name):
    url = "https://api.notion.com/v1/search"
    payload = {"query": query_name, "filter": {"value": "database", "property": "object"}}
    resp = requests.post(url, headers=HEADERS, json=payload)
    if resp.status_code == 200:
        results = resp.json().get("results", [])
        if results:
            return results[0]['id']
    return None

def fetch_and_cache(db_name, output_file):
    print(f"Fetching {db_name}...")
    db_id = find_db_id(db_name)
    if not db_id:
        print(f"Error: Could not find DB '{db_name}'.")
        return

    url = f"https://api.notion.com/v1/databases/{db_id}/query"
    resp = requests.post(url, headers=HEADERS, json={})
    
    if resp.status_code == 200:
        data = resp.json()
        os.makedirs(os.path.dirname(output_file), exist_ok=True)
        with open(output_file, "w") as f:
            json.dump(data.get("results", []), f, indent=2)
        print(f"✅ Cached {db_name} to {output_file}")
    else:
        print(f"Error fetching {db_name}: {resp.text}")

if __name__ == "__main__":
    cache_dir = "/home/node/clawd/data/cache"
    fetch_and_cache("Industries", f"{cache_dir}/industries.json")
    fetch_and_cache("Personas", f"{cache_dir}/personas.json")
    fetch_and_cache("Tasks", f"{cache_dir}/tasks.json")
    
    print("Cache update complete.")
