"""
Script to create Clickbench query views in benchmarks.clickbench schema.

This creates formatted views for all 43 Clickbench benchmark queries,
making them easily accessible as named views.
"""

import os
import sys

from pyiceberg_firestore_gcs import FirestoreCatalog

# Add local paths to sys.path to use local code instead of installed packages
sys.path.insert(0, os.path.join(sys.path[0], ".."))

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = (
    "/Users/justin/Nextcloud/mabel/mabeldev-b37f651c2916.json"
)
os.environ["GCP_PROJECT_ID"] = "mabeldev"
os.environ["FIRESTORE_DATABASE"] = "catalogs"
os.environ["GCS_BUCKET"] = "opteryx_data"


workspace = "benchmarks"

# Create catalog
catalog = FirestoreCatalog(
    workspace,
    firestore_project="mabeldev",
    firestore_database="catalogs",
    gcs_bucket="opteryx_data",
)

# Define the clickbench queries with formatting
# Original queries reference testdata.clickbench_tiny, we'll update to benchmarks.clickbench.hits
queries = [
    (
        "q01",
        "Simple count",
        """
        SELECT COUNT(*) 
        FROM benchmarks.clickbench.hits
    """,
    ),
    (
        "q02",
        "Count with filter",
        """
        SELECT COUNT(*) 
        FROM benchmarks.clickbench.hits 
        WHERE AdvEngineID <> 0
    """,
    ),
    (
        "q03",
        "Basic aggregations",
        """
        SELECT 
            SUM(AdvEngineID), 
            COUNT(*), 
            AVG(ResolutionWidth) 
        FROM benchmarks.clickbench.hits
    """,
    ),
    (
        "q04",
        "Average UserID",
        """
        SELECT AVG(UserID) 
        FROM benchmarks.clickbench.hits
    """,
    ),
    (
        "q05",
        "Count distinct users",
        """
        SELECT COUNT(DISTINCT UserID) 
        FROM benchmarks.clickbench.hits
    """,
    ),
    (
        "q06",
        "Count distinct search phrases",
        """
        SELECT COUNT(DISTINCT SearchPhrase) 
        FROM benchmarks.clickbench.hits
    """,
    ),
    (
        "q07",
        "Date range",
        """
        SELECT 
            MIN(EventDate), 
            MAX(EventDate) 
        FROM benchmarks.clickbench.hits
    """,
    ),
    (
        "q08",
        "Group by with order",
        """
        SELECT 
            AdvEngineID, 
            COUNT(*) 
        FROM benchmarks.clickbench.hits 
        WHERE AdvEngineID <> 0 
        GROUP BY AdvEngineID 
        ORDER BY COUNT(*) DESC
    """,
    ),
    (
        "q09",
        "Top regions by users",
        """
        SELECT 
            RegionID, 
            COUNT(DISTINCT UserID) AS u 
        FROM benchmarks.clickbench.hits 
        GROUP BY RegionID 
        ORDER BY u DESC 
        LIMIT 10
    """,
    ),
    (
        "q10",
        "Region statistics",
        """
        SELECT 
            RegionID, 
            SUM(AdvEngineID), 
            COUNT(*) AS c, 
            AVG(ResolutionWidth), 
            COUNT(DISTINCT UserID) 
        FROM benchmarks.clickbench.hits 
        GROUP BY RegionID 
        ORDER BY c DESC 
        LIMIT 10
    """,
    ),
    (
        "q11",
        "Top mobile phone models",
        """
        SELECT 
            MobilePhoneModel, 
            COUNT(DISTINCT UserID) AS u 
        FROM benchmarks.clickbench.hits 
        WHERE MobilePhoneModel <> '' 
        GROUP BY MobilePhoneModel 
        ORDER BY u DESC 
        LIMIT 10
    """,
    ),
    (
        "q12",
        "Mobile phone and model combinations",
        """
        SELECT 
            MobilePhone, 
            MobilePhoneModel, 
            COUNT(DISTINCT UserID) AS u 
        FROM benchmarks.clickbench.hits 
        WHERE MobilePhoneModel <> '' 
        GROUP BY MobilePhone, MobilePhoneModel 
        ORDER BY u DESC 
        LIMIT 10
    """,
    ),
    (
        "q13",
        "Top search phrases by count",
        """
        SELECT 
            SearchPhrase, 
            COUNT(*) AS c 
        FROM benchmarks.clickbench.hits 
        WHERE SearchPhrase <> '' 
        GROUP BY SearchPhrase 
        ORDER BY c DESC 
        LIMIT 10
    """,
    ),
    (
        "q14",
        "Top search phrases by users",
        """
        SELECT 
            SearchPhrase, 
            COUNT(DISTINCT UserID) AS u 
        FROM benchmarks.clickbench.hits 
        WHERE SearchPhrase <> '' 
        GROUP BY SearchPhrase 
        ORDER BY u DESC 
        LIMIT 10
    """,
    ),
    (
        "q15",
        "Search engine and phrase analysis",
        """
        SELECT 
            SearchEngineID, 
            SearchPhrase, 
            COUNT(*) AS c 
        FROM benchmarks.clickbench.hits 
        WHERE SearchPhrase <> '' 
        GROUP BY SearchEngineID, SearchPhrase 
        ORDER BY c DESC 
        LIMIT 10
    """,
    ),
    (
        "q16",
        "Top users by activity",
        """
        SELECT 
            UserID, 
            COUNT(*) 
        FROM benchmarks.clickbench.hits 
        GROUP BY UserID 
        ORDER BY COUNT(*) DESC 
        LIMIT 10
    """,
    ),
    (
        "q17",
        "User search patterns",
        """
        SELECT 
            UserID, 
            SearchPhrase, 
            COUNT(*) 
        FROM benchmarks.clickbench.hits 
        GROUP BY UserID, SearchPhrase 
        ORDER BY COUNT(*) DESC 
        LIMIT 10
    """,
    ),
    (
        "q18",
        "User search patterns without order",
        """
        SELECT 
            UserID, 
            SearchPhrase, 
            COUNT(*) 
        FROM benchmarks.clickbench.hits 
        GROUP BY UserID, SearchPhrase 
        LIMIT 10
    """,
    ),
    (
        "q19",
        "User activity by minute",
        """
        SELECT 
            UserID, 
            extract(minute FROM EventTime) AS m, 
            SearchPhrase, 
            COUNT(*) 
        FROM benchmarks.clickbench.hits 
        GROUP BY UserID, extract(minute FROM EventTime), SearchPhrase 
        ORDER BY COUNT(*) DESC 
        LIMIT 10
    """,
    ),
    (
        "q20",
        "Find specific user",
        """
        SELECT UserID 
        FROM benchmarks.clickbench.hits 
        WHERE UserID = 435090932899640449
    """,
    ),
    (
        "q21",
        "Google URL pattern count",
        """
        SELECT COUNT(*) 
        FROM benchmarks.clickbench.hits 
        WHERE URL LIKE '%google%'
    """,
    ),
    (
        "q22",
        "Google URLs with search phrases",
        """
        SELECT 
            SearchPhrase, 
            MIN(URL), 
            COUNT(*) AS c 
        FROM benchmarks.clickbench.hits 
        WHERE URL LIKE '%google%' AND SearchPhrase <> '' 
        GROUP BY SearchPhrase 
        ORDER BY c DESC 
        LIMIT 10
    """,
    ),
    (
        "q23",
        "Google title without google domain",
        """
        SELECT 
            SearchPhrase, 
            MIN(URL), 
            MIN(Title), 
            COUNT(*) AS c, 
            COUNT(DISTINCT UserID) 
        FROM benchmarks.clickbench.hits 
        WHERE Title LIKE '%Google%' 
            AND URL NOT LIKE '%.google.%' 
            AND SearchPhrase <> '' 
        GROUP BY SearchPhrase 
        ORDER BY c DESC 
        LIMIT 10
    """,
    ),
    (
        "q24",
        "Recent google URLs",
        """
        SELECT * 
        FROM benchmarks.clickbench.hits 
        WHERE URL LIKE '%google%' 
        ORDER BY EventTime 
        LIMIT 10
    """,
    ),
    (
        "q25",
        "Recent search phrases",
        """
        SELECT SearchPhrase 
        FROM benchmarks.clickbench.hits 
        WHERE SearchPhrase <> '' 
        ORDER BY EventTime 
        LIMIT 10
    """,
    ),
    (
        "q26",
        "Alphabetical search phrases",
        """
        SELECT SearchPhrase 
        FROM benchmarks.clickbench.hits 
        WHERE SearchPhrase <> '' 
        ORDER BY SearchPhrase 
        LIMIT 10
    """,
    ),
    (
        "q27",
        "Search phrases by time and phrase",
        """
        SELECT SearchPhrase 
        FROM benchmarks.clickbench.hits 
        WHERE SearchPhrase <> '' 
        ORDER BY EventTime, SearchPhrase 
        LIMIT 10
    """,
    ),
    (
        "q28",
        "Counter URL analysis",
        """
        SELECT 
            CounterID, 
            AVG(length(URL)) AS l, 
            COUNT(*) AS c 
        FROM benchmarks.clickbench.hits 
        WHERE URL <> '' 
        GROUP BY CounterID 
        HAVING COUNT(*) > 100000 
        ORDER BY l DESC 
        LIMIT 25
    """,
    ),
    (
        "q29",
        "Referer domain analysis",
        """
        SELECT 
            REGEXP_REPLACE(Referer, b'^https?://(?:www\\.)?([^/]+)/.*$', r'\\1') AS k, 
            AVG(length(Referer)) AS l, 
            COUNT(*) AS c, 
            MIN(Referer) 
        FROM benchmarks.clickbench.hits 
        WHERE Referer <> '' 
        GROUP BY REGEXP_REPLACE(Referer, b'^https?://(?:www\\.)?([^/]+)/.*$', r'\\1') 
        HAVING COUNT(*) > 100000 
        ORDER BY l DESC 
        LIMIT 25
    """,
    ),
    (
        "q30",
        "Wide aggregation",
        """
        SELECT 
            SUM(ResolutionWidth), SUM(ResolutionWidth + 1), SUM(ResolutionWidth + 2), 
            SUM(ResolutionWidth + 3), SUM(ResolutionWidth + 4), SUM(ResolutionWidth + 5), 
            SUM(ResolutionWidth + 6), SUM(ResolutionWidth + 7), SUM(ResolutionWidth + 8), 
            SUM(ResolutionWidth + 9), SUM(ResolutionWidth + 10), SUM(ResolutionWidth + 11), 
            SUM(ResolutionWidth + 12), SUM(ResolutionWidth + 13), SUM(ResolutionWidth + 14), 
            SUM(ResolutionWidth + 15), SUM(ResolutionWidth + 16), SUM(ResolutionWidth + 17), 
            SUM(ResolutionWidth + 18), SUM(ResolutionWidth + 19), SUM(ResolutionWidth + 20), 
            SUM(ResolutionWidth + 21), SUM(ResolutionWidth + 22), SUM(ResolutionWidth + 23), 
            SUM(ResolutionWidth + 24), SUM(ResolutionWidth + 25), SUM(ResolutionWidth + 26), 
            SUM(ResolutionWidth + 27), SUM(ResolutionWidth + 28), SUM(ResolutionWidth + 29), 
            SUM(ResolutionWidth + 30), SUM(ResolutionWidth + 31), SUM(ResolutionWidth + 32), 
            SUM(ResolutionWidth + 33), SUM(ResolutionWidth + 34), SUM(ResolutionWidth + 35), 
            SUM(ResolutionWidth + 36), SUM(ResolutionWidth + 37), SUM(ResolutionWidth + 38), 
            SUM(ResolutionWidth + 39), SUM(ResolutionWidth + 40), SUM(ResolutionWidth + 41), 
            SUM(ResolutionWidth + 42), SUM(ResolutionWidth + 43), SUM(ResolutionWidth + 44), 
            SUM(ResolutionWidth + 45), SUM(ResolutionWidth + 46), SUM(ResolutionWidth + 47), 
            SUM(ResolutionWidth + 48), SUM(ResolutionWidth + 49), SUM(ResolutionWidth + 50), 
            SUM(ResolutionWidth + 51), SUM(ResolutionWidth + 52), SUM(ResolutionWidth + 53), 
            SUM(ResolutionWidth + 54), SUM(ResolutionWidth + 55), SUM(ResolutionWidth + 56), 
            SUM(ResolutionWidth + 57), SUM(ResolutionWidth + 58), SUM(ResolutionWidth + 59), 
            SUM(ResolutionWidth + 60), SUM(ResolutionWidth + 61), SUM(ResolutionWidth + 62), 
            SUM(ResolutionWidth + 63), SUM(ResolutionWidth + 64), SUM(ResolutionWidth + 65), 
            SUM(ResolutionWidth + 66), SUM(ResolutionWidth + 67), SUM(ResolutionWidth + 68), 
            SUM(ResolutionWidth + 69), SUM(ResolutionWidth + 70), SUM(ResolutionWidth + 71), 
            SUM(ResolutionWidth + 72), SUM(ResolutionWidth + 73), SUM(ResolutionWidth + 74), 
            SUM(ResolutionWidth + 75), SUM(ResolutionWidth + 76), SUM(ResolutionWidth + 77), 
            SUM(ResolutionWidth + 78), SUM(ResolutionWidth + 79), SUM(ResolutionWidth + 80), 
            SUM(ResolutionWidth + 81), SUM(ResolutionWidth + 82), SUM(ResolutionWidth + 83), 
            SUM(ResolutionWidth + 84), SUM(ResolutionWidth + 85), SUM(ResolutionWidth + 86), 
            SUM(ResolutionWidth + 87), SUM(ResolutionWidth + 88), SUM(ResolutionWidth + 89) 
        FROM benchmarks.clickbench.hits
    """,
    ),
    (
        "q31",
        "Search engine client analysis",
        """
        SELECT 
            SearchEngineID, 
            ClientIP, 
            COUNT(*) AS c, 
            SUM(IsRefresh), 
            AVG(ResolutionWidth) 
        FROM benchmarks.clickbench.hits 
        WHERE SearchPhrase <> '' 
        GROUP BY SearchEngineID, ClientIP 
        ORDER BY c DESC 
        LIMIT 10
    """,
    ),
    (
        "q32",
        "Watch and client analysis",
        """
        SELECT 
            WatchID, 
            ClientIP, 
            COUNT(*) AS c, 
            SUM(IsRefresh), 
            AVG(ResolutionWidth) 
        FROM benchmarks.clickbench.hits 
        WHERE SearchPhrase <> '' 
        GROUP BY WatchID, ClientIP 
        ORDER BY c DESC 
        LIMIT 10
    """,
    ),
    (
        "q33",
        "Watch and client patterns",
        """
        SELECT 
            WatchID, 
            ClientIP, 
            COUNT(*) AS c, 
            SUM(IsRefresh), 
            AVG(ResolutionWidth) 
        FROM benchmarks.clickbench.hits 
        GROUP BY WatchID, ClientIP 
        ORDER BY c DESC 
        LIMIT 10
    """,
    ),
    (
        "q36",
        "Client IP arithmetic",
        """
        SELECT 
            ClientIP, 
            ClientIP - 1, 
            ClientIP - 2, 
            ClientIP - 3, 
            COUNT(*) AS c 
        FROM benchmarks.clickbench.hits 
        GROUP BY ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3 
        ORDER BY c DESC 
        LIMIT 10
    """,
    ),
    (
        "q37",
        "Counter 62 URL pageviews July 2013",
        """
        SELECT 
            URL, 
            COUNT(*) AS PageViews 
        FROM benchmarks.clickbench.hits 
        WHERE CounterID = 62 
            AND EventDate >= '2013-07-01' 
            AND EventDate <= '2013-07-31' 
            AND DontCountHits = 0 
            AND IsRefresh = 0 
            AND URL <> '' 
        GROUP BY URL 
        ORDER BY PageViews DESC 
        LIMIT 10
    """,
    ),
    (
        "q38",
        "Counter 62 title pageviews July 2013",
        """
        SELECT 
            Title, 
            COUNT(*) AS PageViews 
        FROM benchmarks.clickbench.hits 
        WHERE CounterID = 62 
            AND EventDate >= '2013-07-01' 
            AND EventDate <= '2013-07-31' 
            AND DontCountHits = 0 
            AND IsRefresh = 0 
            AND Title <> '' 
        GROUP BY Title 
        ORDER BY PageViews DESC 
        LIMIT 10
    """,
    ),
    (
        "q39",
        "Counter 62 links with offset",
        """
        SELECT 
            URL, 
            COUNT(*) AS PageViews 
        FROM benchmarks.clickbench.hits 
        WHERE CounterID = 62 
            AND EventDate >= '2013-07-01' 
            AND EventDate <= '2013-07-31' 
            AND IsRefresh = 0 
            AND IsLink <> 0 
            AND IsDownload = 0 
        GROUP BY URL 
        ORDER BY PageViews DESC 
        LIMIT 10 
        OFFSET 1000
    """,
    ),
    (
        "q40",
        "Traffic source analysis",
        """
        SELECT 
            TraficSourceID, 
            SearchEngineID, 
            AdvEngineID, 
            CASE WHEN (SearchEngineID = 0 AND AdvEngineID = 0) THEN Referer ELSE '' END AS Src, 
            URL AS Dst, 
            COUNT(*) AS PageViews 
        FROM benchmarks.clickbench.hits 
        WHERE CounterID = 62 
            AND EventDate >= '2013-07-01' 
            AND EventDate <= '2013-07-31' 
            AND IsRefresh = 0 
        GROUP BY TraficSourceID, SearchEngineID, AdvEngineID, 
                 CASE WHEN (SearchEngineID = 0 AND AdvEngineID = 0) THEN Referer ELSE '' END, URL 
        ORDER BY PageViews DESC 
        LIMIT 10 
        OFFSET 1000
    """,
    ),
    (
        "q41",
        "Traffic source URL hash",
        """
        SELECT 
            URLHash, 
            EventDate, 
            COUNT(*) AS PageViews 
        FROM benchmarks.clickbench.hits 
        WHERE CounterID = 62 
            AND EventDate >= '2013-07-01' 
            AND EventDate <= '2013-07-31' 
            AND IsRefresh = 0 
            AND TraficSourceID IN (-1, 6) 
            AND RefererHash = 3594120000172545465 
        GROUP BY URLHash, EventDate 
        ORDER BY PageViews DESC 
        LIMIT 10 
        OFFSET 100
    """,
    ),
    (
        "q42",
        "Window client dimensions",
        """
        SELECT 
            WindowClientWidth, 
            WindowClientHeight, 
            COUNT(*) AS PageViews 
        FROM benchmarks.clickbench.hits 
        WHERE CounterID = 62 
            AND EventDate >= '2013-07-01' 
            AND EventDate <= '2013-07-31' 
            AND IsRefresh = 0 
            AND DontCountHits = 0 
            AND URLHash = 2868770270353813622 
        GROUP BY WindowClientWidth, WindowClientHeight 
        ORDER BY PageViews DESC 
        LIMIT 10 
        OFFSET 10000
    """,
    ),
    (
        "q43",
        "Pageviews by minute",
        """
        SELECT 
            DATE_TRUNC('minute', EventTime) AS M, 
            COUNT(*) AS PageViews 
        FROM benchmarks.clickbench.hits 
        WHERE CounterID = 62 
            AND EventDate >= '2013-07-14' 
            AND EventDate <= '2013-07-15' 
            AND IsRefresh = 0 
            AND DontCountHits = 0 
        GROUP BY DATE_TRUNC('minute', EventTime) 
        ORDER BY M 
        LIMIT 10 
        OFFSET 1000
    """,
    ),
]

print(f"Creating {len(queries)} Clickbench query views in benchmarks.clickbench schema...")
print()

created = 0
recreated = 0
failed = 0

for view_name, description, sql in queries:
    full_name = f"clickbench.{view_name}"

    try:
        # Drop view if it exists (recreating with bug fix)
        if catalog.view_exists(("clickbench", view_name)):
            catalog.drop_view(("clickbench", view_name))
            print(f"  ↻ {full_name} - dropped existing view")

        # Create the view
        catalog.create_view(
            identifier=("clickbench", view_name),
            sql=sql.strip(),
            description=description,
            author="system",
        )

        if catalog.view_exists(("clickbench", view_name)):
            print(f"  ✓ {full_name} - {description}")
            created += 1
        else:
            recreated += 1

    except Exception as e:
        print(f"  ✗ {full_name} - Error: {e}")
        failed += 1

print()
print("=" * 80)
print(f"Done! Created {created} views, failed {failed}")
print("=" * 80)
