# Databricks notebook source
# MAGIC %md
# MAGIC # {{ demo_title }} — Sample Genie Space Setup
# MAGIC
# MAGIC Creates a minimal Genie Space with the gold_customer_summary table.
# MAGIC This file is replaced by `/plan` + `/build-next` with real industry-specific Genie configuration.

# COMMAND ----------

dbutils.widgets.text("catalog", "{{ catalog }}")
catalog = dbutils.widgets.get("catalog")

WAREHOUSE_ID = "{{ warehouse_id }}"

# COMMAND ----------

import json
import requests

# Use notebook execution context token
token = dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiToken().get()
host = dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiUrl().get()

headers = {
    "Authorization": f"Bearer {token}",
    "Content-Type": "application/json",
}

# COMMAND ----------

# MAGIC %md ## Create Genie Space

# COMMAND ----------

genie_config = {
    "title": "{{ demo_title }} — Sample Genie",
    "description": "Ask questions about the sample customer data",
    "warehouse_id": WAREHOUSE_ID,
    "table_identifiers": [
        f"{catalog}.default.gold_customer_summary",
        f"{catalog}.default.silver_customers",
    ],
    "curated_questions": [
        "How many customers do we have?",
        "What is the average customer balance?",
        "Show me all customers with a risk score above 0.7",
    ],
}

resp = requests.post(
    f"{host}/api/2.0/genie/spaces",
    headers=headers,
    json=genie_config,
    timeout=30,
)

if resp.status_code == 200:
    space = resp.json()
    print(f"Genie Space created: {space.get('id', 'unknown')}")
    print(f"Title: {space.get('title', 'unknown')}")
else:
    print(f"Warning: Genie Space creation returned {resp.status_code}: {resp.text[:200]}")
    print("You can create it manually from the Databricks UI")
