{#- Context variables:
   - catalog: str (e.g., "acme_fsi_demo")
   - display_name: str (e.g., "Acme Bank")
   - industry: str (e.g., "fsi")
-#}
# Databricks notebook source

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

# MAGIC %md
# MAGIC # {{ demo_title }} — Catalog Setup
# MAGIC
# MAGIC Creates the Unity Catalog structure for this demo:
# MAGIC - Catalog: `{{ catalog }}`
# MAGIC - Schema: `default` (Claude expands to real schemas during planning)
# MAGIC - Volumes: `landing` and `checkpoints` per schema

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

# Accept catalog name as a widget parameter (overridable from job config)
dbutils.widgets.text("catalog", "{{ catalog }}")
catalog = dbutils.widgets.get("catalog")

print(f"Setting up catalog: {catalog}")

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

# MAGIC %md
# MAGIC ## Step 1: Create Catalog

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

spark.sql(f"CREATE CATALOG IF NOT EXISTS {catalog}")
spark.sql(f"USE CATALOG {catalog}")
print(f"Catalog '{catalog}' ready.")

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

# MAGIC %md
# MAGIC ## Step 2: Create Schemas
# MAGIC
# MAGIC The `default` schema is created as a starting point.
# MAGIC Claude will expand this to real domain schemas during Phase 0 planning.

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

# Schemas to create — Claude updates this list after planning
schemas = ["default"]

for schema in schemas:
    spark.sql(f"CREATE SCHEMA IF NOT EXISTS {catalog}.{schema}")
    print(f"  Schema '{catalog}.{schema}' ready.")

print(f"\nCreated {len(schemas)} schema(s).")

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

# MAGIC %md
# MAGIC ## Step 3: Create Volumes
# MAGIC
# MAGIC Each schema gets two volumes:
# MAGIC - **landing**: raw data files written by data generation notebooks
# MAGIC - **checkpoints**: DLT schema inference checkpoints

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

volume_types = ["landing", "checkpoints"]

for schema in schemas:
    for vol in volume_types:
        spark.sql(
            f"CREATE VOLUME IF NOT EXISTS {catalog}.{schema}.{vol}"
        )
        print(f"  Volume '{catalog}.{schema}.{vol}' ready.")

print(f"\nCreated {len(schemas) * len(volume_types)} volume(s).")

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

# MAGIC %md
# MAGIC ## Validation

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

print(f"\n{'='*60}")
print(f"Catalog Setup Complete: {catalog}")
print(f"{'='*60}")

# List schemas
print("\nSchemas:")
for row in spark.sql(f"SHOW SCHEMAS IN {catalog}").collect():
    print(f"  - {catalog}.{row.databaseName}")

# List volumes per schema
print("\nVolumes:")
for schema in schemas:
    for row in spark.sql(f"SHOW VOLUMES IN {catalog}.{schema}").collect():
        print(f"  - {catalog}.{schema}.{row.volume_name}")

print(f"\nSetup complete. Ready for data generation.")
