"""
Visual Inspection Script — test RBF flow model variant.
Uses the demo data with RBF instead of MDN flow model.

THIS WILL CALL THE OPENAI EMBEDDING API (only if no cache exists).

Run: python test/test_visual_rbf.py
"""

import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from tracescope import TraceScopeConfig, AnalysisPipeline, auto_import, launch_renderer

DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", "tools", "prm_demo_40paths.json")
CACHE_PATH = os.path.join(os.path.dirname(__file__), "cache", "rbf_test")


def main():
    print("Loading demo data...")
    session = auto_import(DEMO_PATH)
    print(f"Loaded {len(session)} entries")

    config = TraceScopeConfig()
    pipeline = AnalysisPipeline(config)

    print("Running pipeline with RBF flow model...")
    print("(Will use cache if available)")

    result = pipeline.analyze(
        session,
        train_flow=True,
        flow_mode="rbf",
        rbf_kernel="thin_plate_spline",
        rbf_smoothing=0.1,
        cache_path=CACHE_PATH,
        progress_callback=lambda stage, pct: print(f"  [{stage}] {pct*100:.0f}%"),
    )

    print(f"\nResult: {result.n_entries} entries, {result.clusters.n_clusters} clusters")
    print(f"Flow trained: {result.flow_model_trained}")
    print(f"Velocity grid: {result.velocity_grid.shape if result.velocity_grid is not None else None}")

    print("\nLaunching renderer with RBF flow...")
    launch_renderer(result, explainer=pipeline.explainer)


if __name__ == "__main__":
    main()
