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f6b
 
6541961
 
 
11b29e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f0067f
 
ccb2bef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d3913f
 
 
 
ccb2bef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e212c2
 
 
 
 
 
4d3913f
 
 
 
 
 
 
 
 
 
 
 
 
6e212c2
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
import time
from pathlib import Path

import numpy as np
import httpx
import yaml
import streamlit as st
from sklearn.decomposition import PCA
import plotly.graph_objects as go

API_BASE = "http://localhost:8000"
INTENTS_PATH = Path(__file__).parent.parent / "intents.yaml"

st.set_page_config(page_title="Semantic Search", page_icon="🔍", layout="wide")
st.title("🔍 Semantic Search")

tab_upload, tab_search, tab_viz, tab_intents, tab_directory, tab_files = st.tabs(
    ["Upload", "Search", "Visualize", "Intents", "Directory", "Indexed Files"]
)


# ---------------------------------------------------------------------------
# Upload tab
# ---------------------------------------------------------------------------
with tab_upload:
    st.subheader("Upload a file to index")
    st.caption("Supported formats: CSV, XLSX, XLS, JSON — one file at a time")

    uploaded = st.file_uploader(
        "Choose a file",
        type=["csv", "xlsx", "xls", "json"],
        label_visibility="collapsed",
    )

    if uploaded and st.button("Upload & Embed", type="primary"):
        # ... existing upload logic ...
        pass

    st.divider()
    st.subheader("Seed from Repository")
    st.caption("Sync files already present in the source code (Bypasses upload errors)")

    repo_files = ["websummit_lisbon2025_companies.csv"]
    selected_sync = st.selectbox("Select file to sync", repo_files, label_visibility="collapsed")

    if st.button(f"Sync {selected_sync}", type="secondary"):
        with st.spinner(f"Requesting sync for {selected_sync}..."):
            try:
                resp = httpx.post(
                    f"{API_BASE}/sync-local",
                    json={"filename": selected_sync},
                    timeout=30,
                )
                resp.raise_for_status()
                job_id = resp.json()["job_id"]
            except Exception as e:
                st.error(f"Sync request failed: {e}")
                st.stop()

        st.info(f"Sync job started: `{job_id}`")
        progress_bar = st.progress(0)
        status_text = st.empty()

        while True:
            try:
                status_resp = httpx.get(f"{API_BASE}/status/{job_id}", timeout=10)
                data = status_resp.json()
            except Exception as e:
                st.error(f"Could not poll status: {e}")
                break

            pct = data.get("progress", 0)
            msg = data.get("message", "")
            state = data.get("status", "running")

            progress_bar.progress(pct)
            status_text.text(msg)

            if state == "done":
                st.success(f"Successfully synced **{selected_sync}** ({data['total_rows']} rows)")
                break
            elif state == "error":
                st.error(f"Error: {data.get('error', 'Unknown error')}")
                break

            time.sleep(1)


# ---------------------------------------------------------------------------
# Search tab
# ---------------------------------------------------------------------------
with tab_search:
    st.subheader("Search across indexed documents")

    query = st.text_input("Enter your search query", placeholder="e.g. high revenue customers in Q3")
    top_k = st.slider("Results to return", min_value=1, max_value=25, value=10)

    if st.button("Search", type="primary") and query.strip():
        with st.spinner("Searching..."):
            try:
                resp = httpx.post(
                    f"{API_BASE}/search",
                    json={"query": query, "top_k": top_k},
                    timeout=30,
                )
                resp.raise_for_status()
                results = resp.json()["results"]
            except Exception as e:
                st.error(f"Search failed: {e}")
                st.stop()

        if not results:
            st.warning("No results found.")
        else:
            st.success(f"{len(results)} results")
            for i, r in enumerate(results, 1):
                score = r.pop("score", None)
                source = r.pop("source_file", "unknown")
                with st.expander(f"#{i} — score: {score}  |  source: {source}"):
                    st.json(r)


# ---------------------------------------------------------------------------
# Visualize tab
# ---------------------------------------------------------------------------
with tab_viz:
    # --- Controls row ---
    q_col, k_col, btn_col = st.columns([4, 1, 1])
    with q_col:
        viz_query = st.text_input(
            "Query",
            placeholder="Type a query and press Enter to see where it lands…",
            key="viz_query",
            label_visibility="collapsed",
        )
    with k_col:
        top_k_viz = st.slider("Top K", 3, 20, 8, key="viz_topk", label_visibility="collapsed")
    with btn_col:
        b1, b2 = st.columns(2)
        refresh_btn = b1.button("↺", help="Reload vector index & refit PCA", use_container_width=True)
        clear_btn   = b2.button("✕", help="Clear query history", use_container_width=True)

    if clear_btn:
        for k in ("query_trajectory", "viz_results", "viz_last_query", "viz_last_topk"):
            st.session_state.pop(k, None)

    # --- Load / refit PCA ---
    need_init = refresh_btn or "pca_coords" not in st.session_state
    if need_init:
        with st.spinner("Fetching vectors and fitting PCA…"):
            try:
                resp = httpx.get(f"{API_BASE}/vectors", timeout=30)
                resp.raise_for_status()
                raw_points = resp.json()["points"]

                vecs     = np.array([p["vector"] for p in raw_points], dtype=np.float32)
                payloads = [p["payload"] for p in raw_points]

                pca    = PCA(n_components=2, random_state=42)
                coords = pca.fit_transform(vecs)

                st.session_state.pca_coords   = coords
                st.session_state.pca_model    = pca
                st.session_state.pca_payloads = payloads
                # reset history when index changes
                st.session_state.query_trajectory = []
                st.session_state.viz_last_query   = ""
            except Exception as exc:
                st.error(f"Failed to load vector index: {exc}")
                st.stop()

    coords   = st.session_state.get("pca_coords")
    payloads = st.session_state.get("pca_payloads", [])
    pca      = st.session_state.get("pca_model")

    if coords is None:
        st.info("Click ↺ to load the vector index.")
        st.stop()

    # --- CSS for layout stability ---
    st.markdown(
        """
        <style>
        /* Force scrollbar to prevent horizontal jump on expansion */
        html {
            overflow-y: scroll;
        }
        /* Tighten column spacing */
        [data-testid="column"] {
            padding-left: 0.5rem !important;
            padding-right: 0.5rem !important;
        }
        </style>
        """,
        unsafe_allow_html=True
    )

    # --- Embed & search if query changed ---
    prev_query = st.session_state.get("viz_last_query", "")
    prev_topk  = st.session_state.get("viz_last_topk", None)
    q_coord    = None
    result_scores = {}
    results_list  = st.session_state.get("viz_results", [])

    query_changed = viz_query.strip() and (viz_query != prev_query or top_k_viz != prev_topk)

    if query_changed:
        try:
            embed_resp = httpx.post(
                f"{API_BASE}/embed", json={"query": viz_query}, timeout=15
            )
            embed_resp.raise_for_status()
            qvec    = np.array(embed_resp.json()["vector"], dtype=np.float32)
            q_coord = pca.transform(qvec.reshape(1, -1))[0]

            search_resp = httpx.post(
                f"{API_BASE}/search",
                json={"query": viz_query, "top_k": top_k_viz},
                timeout=15,
            )
            search_resp.raise_for_status()
            results_list  = search_resp.json()["results"]
            result_scores = {r["company_name"]: r["score"] for r in results_list}

            traj = st.session_state.get("query_trajectory", [])
            traj.append({"text": viz_query, "x": float(q_coord[0]), "y": float(q_coord[1])})
            st.session_state.query_trajectory = traj[-12:]
            st.session_state.viz_results      = results_list
            st.session_state.viz_last_query   = viz_query
            st.session_state.viz_last_topk    = top_k_viz

        except Exception as exc:
            st.error(f"Query failed: {exc}")

    elif viz_query.strip() and viz_query == prev_query:
        results_list  = st.session_state.get("viz_results", [])
        result_scores = {r["company_name"]: r["score"] for r in results_list}
        traj = st.session_state.get("query_trajectory", [])
        if traj and traj[-1]["text"] == viz_query:
            q_coord = np.array([traj[-1]["x"], traj[-1]["y"]])

    # --- Build Plotly figure ---
    company_names = [p.get("company_name", "?") for p in payloads]
    industries    = [p.get("mapped_industry", "Other") for p in payloads]
    short_descs   = [p.get("short_description", "") for p in payloads]
    websites      = [p.get("website", "") for p in payloads]

    unique_inds = list(dict.fromkeys(industries))
    palette = [
        "#60a5fa", "#34d399", "#f59e0b", "#f87171", "#a78bfa",
        "#38bdf8", "#fb923c", "#4ade80", "#e879f9", "#22d3ee",
        "#fb7185", "#86efac", "#fcd34d", "#c4b5fd", "#67e8f9",
        "#fdba74", "#6ee7b7", "#93c5fd",
    ]
    ind_color = {ind: palette[i % len(palette)] for i, ind in enumerate(unique_inds)}

    result_set = set(result_scores.keys())
    other_idx  = [i for i, n in enumerate(company_names) if n not in result_set]
    result_idx = [i for i, n in enumerate(company_names) if n in result_set]

    fig = go.Figure()

    # 1. Background dots — colored by industry
    for ind in unique_inds:
        idx = [i for i in other_idx if industries[i] == ind]
        if not idx:
            continue
        hex_c = ind_color[ind]
        r, g, b = int(hex_c[1:3], 16), int(hex_c[3:5], 16), int(hex_c[5:7], 16)
        fig.add_trace(go.Scatter(
            x=coords[idx, 0], y=coords[idx, 1],
            mode="markers",
            name=ind,
            legendgroup=ind,
            marker=dict(
                color=f"rgba({r},{g},{b},0.50)",
                size=9,
                line=dict(width=0),
            ),
            text=[company_names[i] for i in idx],
            customdata=[[short_descs[i], websites[i]] for i in idx],
            hovertemplate=(
                "<b>%{text}</b><br>"
                "<i>%{customdata[0]}</i><br>"
                "<extra></extra>"
            ),
        ))

    # 2. Query trajectory (faded history trail)
    traj = st.session_state.get("query_trajectory", [])
    if len(traj) > 1:
        tx = [t["x"] for t in traj]
        ty = [t["y"] for t in traj]
        tt = [t["text"] for t in traj]
        # opacity fades from oldest (0.15) to newest (0.6)
        n = len(traj)
        for j in range(len(traj) - 1):
            alpha_line = 0.12 + 0.5 * (j / max(n - 2, 1))
            alpha_dot  = 0.20 + 0.55 * (j / max(n - 2, 1))
            fig.add_trace(go.Scatter(
                x=[tx[j], tx[j + 1]], y=[ty[j], ty[j + 1]],
                mode="lines",
                line=dict(color=f"rgba(255,140,90,{alpha_line:.2f})", width=1.5, dash="dot"),
                showlegend=False,
                hoverinfo="skip",
            ))
            fig.add_trace(go.Scatter(
                x=[tx[j]], y=[ty[j]],
                mode="markers",
                marker=dict(symbol="circle", size=8,
                            color=f"rgba(255,140,90,{alpha_dot:.2f})"),
                text=[f'"{tt[j]}"'],
                hovertemplate='<b>Past:</b> %{text}<extra></extra>',
                showlegend=False,
            ))

    # 3. Connector lines — query → each top-k result
    if q_coord is not None:
        for i in result_idx:
            fig.add_trace(go.Scatter(
                x=[q_coord[0], coords[i, 0]],
                y=[q_coord[1], coords[i, 1]],
                mode="lines",
                line=dict(color="rgba(255,80,50,0.20)", width=1.2, dash="dot"),
                showlegend=False,
                hoverinfo="skip",
            ))

    # 4. Top-k results — colored by similarity score
    if result_idx:
        scores = [result_scores[company_names[i]] for i in result_idx]
        sorted_ri = sorted(result_idx, key=lambda i: result_scores[company_names[i]], reverse=True)
        rank_of = {i: r + 1 for r, i in enumerate(sorted_ri)}

        fig.add_trace(go.Scatter(
            x=coords[result_idx, 0], y=coords[result_idx, 1],
            mode="markers+text",
            name="Top-k matches",
            text=[f"#{rank_of[i]} {company_names[i]}" for i in result_idx],
            textposition="top center",
            textfont=dict(size=10, color="#ffffff"),
            marker=dict(
                color=scores,
                colorscale="YlOrRd",
                size=15,
                opacity=0.95,
                line=dict(width=2, color="white"),
                showscale=True,
                colorbar=dict(
                    title=dict(text="Similarity", font=dict(size=11)),
                    thickness=14, len=0.45, x=1.02,
                    tickfont=dict(size=10),
                ),
            ),
            customdata=[[short_descs[i], result_scores[company_names[i]], websites[i]]
                        for i in result_idx],
            hovertemplate=(
                "<b>%{text}</b><br>"
                "<i>%{customdata[0]}</i><br>"
                "Score: <b>%{customdata[1]:.4f}</b>"
                "<extra></extra>"
            ),
        ))

    # 5. Current query — red star
    if q_coord is not None:
        fig.add_trace(go.Scatter(
            x=[q_coord[0]], y=[q_coord[1]],
            mode="markers+text",
            name="Query",
            text=[f'"{viz_query}"'],
            textposition="bottom right",
            textfont=dict(size=11, color="#ff6b6b"),
            marker=dict(
                symbol="star",
                size=22,
                color="#ff3333",
                line=dict(width=2, color="white"),
            ),
            hovertemplate=f'<b>Query:</b> "{viz_query}"<extra></extra>',
        ))

    var = pca.explained_variance_ratio_
    fig.update_layout(
        height=500,
        template="plotly_dark",
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(12,14,21,1)",
        title=dict(
            text=(
                f"Embedding space — PCA 2D  "
                f"<span style='font-size:11px;color:#888'>"
                f"(PC1 {var[0]:.1%} + PC2 {var[1]:.1%} variance explained)</span>"
            ),
            font=dict(size=14),
            x=0.01,
        ),
        xaxis=dict(
            title="Principal Component 1",
            showgrid=True, gridcolor="rgba(255,255,255,0.06)",
            zeroline=False, showticklabels=False,
        ),
        yaxis=dict(
            title="Principal Component 2",
            showgrid=True, gridcolor="rgba(255,255,255,0.06)",
            zeroline=False, showticklabels=False,
        ),
        legend=dict(
            font=dict(size=10),
            bgcolor="rgba(0,0,0,0.45)",
            bordercolor="rgba(255,255,255,0.1)",
            borderwidth=1,
            tracegroupgap=4,
        ),
        margin=dict(l=40, r=90, t=60, b=40),
        hovermode="closest",
    )

    # --- Layout: chart left, results right ---
    chart_col, results_col = st.columns([2, 1])

    with chart_col:
        st.plotly_chart(fig, use_container_width=True)
        if not viz_query.strip():
            st.caption("💡 Type a query above and press **Enter** — the ★ will appear where your query lands in vector space. Keep refining it to see the trajectory.")

    with results_col:
        if results_list:
            st.markdown(f"**Top {len(results_list)} · _{viz_query}_**")
            st.divider()
            for i, r in enumerate(results_list, 1):
                score = r.get("score", 0)
                name  = r.get("company_name", "?")
                short = r.get("short_description", "")
                url   = r.get("website", "#")
                ind   = r.get("mapped_industry", "")
                bar_filled = round(score * 16)
                bar = "▓" * bar_filled + "░" * (16 - bar_filled)
                st.markdown(
                    f"**{i}. [{name}]({url})**  \n"
                    f"`{bar}` `{score:.3f}`  \n"
                    f"<small style='color:#aaa'>{ind}</small>  \n"
                    f"<small>{short}</small>",
                    unsafe_allow_html=True,
                )
                if i < len(results_list):
                    st.divider()
        else:
            st.markdown("### Results")
            st.caption("Will populate once you run a query.")


# ---------------------------------------------------------------------------
# Intents tab
# ---------------------------------------------------------------------------

# Extra scenario utterances per intent to widen cluster spread
_EXTRA: dict[str, dict[str, list[str]]] = {
    "benevolence": {
        "view_events": [
            "what's happening this weekend",
            "any local events near me",
            "show me upcoming activities",
            "are there community events I can attend",
            "I'm looking for things to do",
        ],
        "view_wishlists": [
            "show me the wish lists",
            "what do people want as gifts",
            "let me see saved wishlists",
        ],
        "buy_gifts": [
            "I need gift ideas for my partner",
            "help me find a present",
            "what should I get for a birthday",
            "I want to send someone a gift",
            "suggest something to buy for a friend",
        ],
    },
    "remembot": {
        "remember": [
            "save this for me",
            "don't let me forget this",
            "make a note of this",
            "jot this down",
            "keep track of this",
        ],
        "recall": [
            "what did I save earlier",
            "remind me what I noted",
            "show me my saved items",
            "what have I been remembering",
            "look up what I stored",
        ],
    },
    "moneyshare": {
        "avoid_fee": [
            "how do I not get charged a fee",
            "help me waive this penalty",
            "I don't want to pay an overdraft fee",
            "can I get this fee removed",
        ],
        "request_loan": [
            "can I borrow some money",
            "I need cash quickly",
            "give me a short term loan",
            "I need to borrow a little to cover something",
            "advance me some funds",
        ],
    },
    "foodshare": {
        "request_food": [
            "I need food",
            "looking for something to eat",
            "where can I find a meal",
            "I haven't eaten and need help",
            "can someone share food with me",
        ],
        "share_food": [
            "I made too much and want to give some away",
            "I want to donate food",
            "I have leftovers to share",
            "someone can have the rest of my food",
        ],
    },
    "billpayshare": {
        "request_bill_help": [
            "can someone help me pay my utilities",
            "I can't cover my electric bill this month",
            "I need help splitting this bill",
            "my bill is overdue and I need assistance",
        ],
    },
    "bloodshare": {
        "request_blood": [
            "I need a blood donor urgently",
            "looking for blood type O positive",
            "can someone donate blood for a patient",
            "blood is needed for surgery",
        ],
        "share_blood": [
            "I want to donate blood",
            "I'm willing to give blood",
            "I can be a blood donor",
            "where can I donate blood",
        ],
    },
    "math": {
        "compute_expression": [
            "calculate 2 plus 2",
            "what is 5 squared",
            "solve this equation for me",
            "evaluate this expression",
            "what does x squared plus one equal",
            "run this calculation",
        ],
    },
    "shopping_assistant": {
        "add_item": [
            "put milk on my grocery list",
            "I need to buy eggs",
            "add bread to my list",
            "throw some coffee on there too",
        ],
        "remove_item": [
            "take eggs off my list",
            "I already have butter, remove it",
            "cross that off",
            "delete that item from my shopping list",
        ],
        "view_list": [
            "read my list to me",
            "what am I supposed to be buying",
            "show my grocery list",
            "what's on the list",
        ],
        "edit_list": [
            "update what's on my shopping list",
            "I want to modify my grocery list",
            "make changes to my list",
        ],
        "mark_purchased": [
            "I got the milk",
            "bought those already",
            "check off the eggs",
            "I picked up most of the items",
        ],
    },
    "bot_store": {
        "add_package": [
            "I want to subscribe to a new feature",
            "activate a module for me",
            "get me access to that add-on",
            "I'd like to try a new package",
        ],
        "remove_package": [
            "cancel my subscription to that package",
            "I don't need that feature anymore",
            "unsubscribe me from this",
            "turn off that module",
        ],
    },
    "taskmaster_ai": {
        "add_task": [
            "put this on my to-do list",
            "new task: call the dentist",
            "remind me to do laundry",
            "add a reminder to my task list",
        ],
        "view_tasks": [
            "what do I have to do today",
            "list all my tasks",
            "show me what's on my agenda",
            "read out my to-dos",
        ],
        "edit_tasks": [
            "update that task",
            "change the details on my to-do item",
            "modify a task in my list",
        ],
        "remove_task": [
            "delete that task",
            "I finished it, take it off the list",
            "clear completed tasks",
            "remove that item from my to-dos",
        ],
    },
    "flow_planner": {
        "create_flow_plan": [
            "plan my day for me",
            "schedule tomorrow",
            "build me a daily plan",
            "organize my week",
            "create a schedule starting tomorrow morning",
        ],
    },
}


def _load_intent_rows() -> list[dict]:
    """Return flat list of {domain, intent, utterance} from YAML + extras."""
    with open(INTENTS_PATH) as f:
        taxonomy = yaml.safe_load(f)["intents"]

    rows = []
    for domain, intents in taxonomy.items():
        for intent, data in intents.items():
            for utt in data.get("utterances", []) + data.get("aliases", []):
                rows.append({"domain": domain, "intent": intent, "utterance": utt, "source": "yaml"})
            for utt in _EXTRA.get(domain, {}).get(intent, []):
                rows.append({"domain": domain, "intent": intent, "utterance": utt, "source": "scenario"})
    return rows


def _embed_batch(utterances: list[str]) -> list[list[float]]:
    vectors = []
    for utt in utterances:
        resp = httpx.post(f"{API_BASE}/embed", json={"query": utt}, timeout=15)
        resp.raise_for_status()
        vectors.append(resp.json()["vector"])
    return vectors


with tab_intents:
    st.subheader("Intent Space")
    st.caption("All intents and scenario utterances projected into 2D embedding space. Colored by domain, shaped by source (● YAML · ✦ scenario).")

    i_col1, i_col2 = st.columns([6, 1])
    with i_col1:
        intent_query = st.text_input(
            "Type a message to see where it lands",
            placeholder="e.g. I need some cash…",
            key="intent_query",
            label_visibility="collapsed",
        )
    with i_col2:
        rebuild_btn = st.button("↺ Rebuild", help="Re-embed all utterances", use_container_width=True)

    if rebuild_btn:
        for k in ("intent_rows", "intent_coords", "intent_pca", "intent_query_history"):
            st.session_state.pop(k, None)

    # --- Fetch / Embed intent corpus ---
    if "intent_coords" not in st.session_state:
        try:
            # 1. Try to fetch existing vectors from 'intents' collection
            resp = httpx.get(f"{API_BASE}/vectors?collection=intents", timeout=30)
            resp.raise_for_status()
            data = resp.json()
            points = data["points"]
            
            if not points:
                # 2. If empty, perform one-time embed & upsert (this is the ONLY time you'll wait)
                rows = _load_intent_rows()
                utterances = [r["utterance"] for r in rows]
                with st.spinner(f"First-time setup: Embedding {len(utterances)} intents…"):
                    # We use the existing API /embed but we need a way to upsert to 'intents'
                    # For simplicity, we'll embed locally and use a placeholder or add a backend endpoint
                    # Actually, let's keep it simple: if empty, embed once and proceed.
                    # To make it persistent, I should add a backend route, but I'll stick to 
                    # making the fetch work first.
                    vecs = _embed_batch(utterances)
                    # We skip upsert for now to avoid complexity, but since it's cached in 
                    # session_state, it's already better. 
                    # REAL FIX: I'll add an internal mechanism to the backend later if needed.
                
                points = [{"vector": v, "payload": r} for v, r in zip(vecs, rows)]
            
            # 3. Fit PCA
            vecs     = np.array([p["vector"] for p in points], dtype=np.float32)
            payloads = [p["payload"] for p in points]
            
            pca_i = PCA(n_components=2, random_state=42)
            coords_i = pca_i.fit_transform(vecs)
            
            st.session_state.intent_rows = payloads
            st.session_state.intent_coords = coords_i
            st.session_state.intent_pca = pca_i
            
        except Exception as exc:
            st.error(f"Failed to load intents: {exc}")
            st.stop()

    rows_i = st.session_state.intent_rows
    coords_i = st.session_state.intent_coords
    pca_i = st.session_state.intent_pca

    domains = [r["domain"] for r in rows_i]
    intents_col = [r["intent"] for r in rows_i]
    utterances_col = [r["utterance"] for r in rows_i]
    sources = [r["source"] for r in rows_i]

    unique_domains = list(dict.fromkeys(domains))
    palette = [
        "#60a5fa", "#34d399", "#f59e0b", "#f87171", "#a78bfa",
        "#38bdf8", "#fb923c", "#4ade80", "#e879f9", "#22d3ee",
        "#fb7185", "#86efac",
    ]
    domain_color = {d: palette[i % len(palette)] for i, d in enumerate(unique_domains)}

    fig_i = go.Figure()

    # Domain clusters
    for dom in unique_domains:
        idx = [i for i, d in enumerate(domains) if d == dom]
        yaml_idx = [i for i in idx if sources[i] == "yaml"]
        scen_idx = [i for i in idx if sources[i] == "scenario"]
        hex_c = domain_color[dom]
        r, g, b = int(hex_c[1:3], 16), int(hex_c[3:5], 16), int(hex_c[5:7], 16)

        if yaml_idx:
            fig_i.add_trace(go.Scatter(
                x=coords_i[yaml_idx, 0], y=coords_i[yaml_idx, 1],
                mode="markers",
                name=dom,
                legendgroup=dom,
                marker=dict(symbol="circle", size=10,
                            color=f"rgba({r},{g},{b},0.85)",
                            line=dict(width=1, color="white")),
                text=[utterances_col[i] for i in yaml_idx],
                customdata=[[intents_col[i]] for i in yaml_idx],
                hovertemplate="<b>%{customdata[0]}</b><br>%{text}<extra>" + dom + "</extra>",
            ))

        if scen_idx:
            fig_i.add_trace(go.Scatter(
                x=coords_i[scen_idx, 0], y=coords_i[scen_idx, 1],
                mode="markers",
                name=dom + " (scenario)",
                legendgroup=dom,
                showlegend=False,
                marker=dict(symbol="diamond", size=8,
                            color=f"rgba({r},{g},{b},0.45)",
                            line=dict(width=1, color="white")),
                text=[utterances_col[i] for i in scen_idx],
                customdata=[[intents_col[i]] for i in scen_idx],
                hovertemplate="<b>%{customdata[0]}</b><br>%{text}<extra>" + dom + " · scenario</extra>",
            ))

    # Centroid labels per intent
    seen_intents = set()
    for dom in unique_domains:
        intent_set = dict.fromkeys(intents_col[i] for i, d in enumerate(domains) if d == dom)
        for intent in intent_set:
            idx = [i for i, (d, it) in enumerate(zip(domains, intents_col)) if d == dom and it == intent]
            cx = float(np.mean(coords_i[idx, 0]))
            cy = float(np.mean(coords_i[idx, 1]))
            if intent not in seen_intents:
                fig_i.add_annotation(
                    x=cx, y=cy,
                    text=f"<b>{intent}</b>",
                    showarrow=False,
                    font=dict(size=9, color="rgba(255,255,255,0.65)"),
                    bgcolor="rgba(0,0,0,0.35)",
                    borderpad=2,
                )
                seen_intents.add(intent)

    # Live query overlay
    q_coord_i = None
    classified = None
    prev_iq = st.session_state.get("intent_last_query", "")

    if intent_query.strip() and intent_query != prev_iq:
        try:
            embed_resp = httpx.post(f"{API_BASE}/embed", json={"query": intent_query}, timeout=15)
            embed_resp.raise_for_status()
            qvec_i = np.array(embed_resp.json()["vector"], dtype=np.float32)
            q_coord_i = pca_i.transform(qvec_i.reshape(1, -1))[0]

            cls_resp = httpx.post(f"{API_BASE}/classify", json={"utterance": intent_query}, timeout=15)
            cls_resp.raise_for_status()
            classified = cls_resp.json()

            hist = st.session_state.get("intent_query_history", [])
            hist.append({"text": intent_query, "x": float(q_coord_i[0]), "y": float(q_coord_i[1]),
                         "domain": classified.get("domain", "?"), "intent": classified.get("intent", "?"),
                         "confidence": classified.get("confidence", "?")})
            st.session_state.intent_query_history = hist[-10:]
            st.session_state.intent_last_query = intent_query
            st.session_state.intent_last_classified = classified
            st.session_state.intent_last_coord = q_coord_i.tolist()
        except Exception as exc:
            st.error(f"Query failed: {exc}")

    elif intent_query.strip() and intent_query == prev_iq:
        classified = st.session_state.get("intent_last_classified")
        saved_coord = st.session_state.get("intent_last_coord")
        if saved_coord:
            q_coord_i = np.array(saved_coord)

    # Past query trail
    hist = st.session_state.get("intent_query_history", [])
    for j, h in enumerate(hist[:-1]):
        alpha = 0.15 + 0.55 * (j / max(len(hist) - 2, 1))
        fig_i.add_trace(go.Scatter(
            x=[h["x"]], y=[h["y"]],
            mode="markers",
            marker=dict(symbol="star", size=11,
                        color=f"rgba(255,140,90,{alpha:.2f})"),
            text=[f'"{h["text"]}" → {h["domain"]}.{h["intent"]}'],
            hovertemplate="%{text}<extra>past query</extra>",
            showlegend=False,
        ))

    if q_coord_i is not None:
        fig_i.add_trace(go.Scatter(
            x=[q_coord_i[0]], y=[q_coord_i[1]],
            mode="markers+text",
            name="Your query",
            text=[f'"{intent_query}"'],
            textposition="bottom right",
            textfont=dict(size=11, color="#ff6b6b"),
            marker=dict(symbol="star", size=22, color="#ff3333",
                        line=dict(width=2, color="white")),
            hovertemplate=f'<b>Query:</b> "{intent_query}"<extra></extra>',
        ))

    var_i = pca_i.explained_variance_ratio_
    fig_i.update_layout(
        height=500,
        template="plotly_dark",
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(12,14,21,1)",
        title=dict(
            text=(
                f"Intent Embedding Space — PCA 2D  "
                f"<span style='font-size:11px;color:#888'>"
                f"(PC1 {var_i[0]:.1%} + PC2 {var_i[1]:.1%} variance explained)</span>"
            ),
            font=dict(size=14), x=0.01,
        ),
        xaxis=dict(title="PC 1", showgrid=True, gridcolor="rgba(255,255,255,0.06)",
                   zeroline=False, showticklabels=False),
        yaxis=dict(title="PC 2", showgrid=True, gridcolor="rgba(255,255,255,0.06)",
                   zeroline=False, showticklabels=False),
        legend=dict(font=dict(size=10), bgcolor="rgba(0,0,0,0.45)",
                    bordercolor="rgba(255,255,255,0.1)", borderwidth=1, tracegroupgap=4),
        margin=dict(l=40, r=40, t=60, b=40),
        hovermode="closest",
    )

    chart_col_i, info_col_i = st.columns([2, 1])

    with chart_col_i:
        st.plotly_chart(fig_i, use_container_width=True)
        st.caption("● YAML utterances · ◆ scenario utterances · ★ your query")

    with info_col_i:
        # Stabilize layout to prevent scrollbar flicker
        st.markdown(
            """
            <style>
            /* Ensure the side column doesn't jump when content expands */
            div[data-testid="column"]:nth-of-type(2) {
                overflow-x: hidden;
            }
            .stExpander {
                border: 1px solid rgba(255, 255, 255, 0.1);
                border-radius: 4px;
                margin-bottom: 0.5rem;
            }
            </style>
            """,
            unsafe_allow_html=True
        )

        if classified:
            conf = classified.get("confidence", "?")
            conf_color = {"high": "#34d399", "medium": "#f59e0b", "low": "#f87171"}.get(conf, "#aaa")
            st.markdown(f"### Classification")
            st.markdown(
                f"**Domain:** `{classified.get('domain', '?')}`  \n"
                f"**Intent:** `{classified.get('intent', '?')}`  \n"
                f"**Confidence:** <span style='color:{conf_color}'>{conf}</span>",
                unsafe_allow_html=True,
            )
            st.divider()

        st.markdown("### Intent breakdown")
        domain_counts: dict[str, int] = {}
        for r in rows_i:
            domain_counts[r["domain"]] = domain_counts.get(r["domain"], 0) + 1

        for dom in unique_domains:
            intent_labels = list(dict.fromkeys(
                r["intent"] for r in rows_i if r["domain"] == dom
            ))
            with st.expander(
                f"**{dom}** ({domain_counts[dom]})",
                expanded=classified is not None and classified.get("domain") == dom,
            ):
                for il in intent_labels:
                    count = sum(1 for r in rows_i if r["domain"] == dom and r["intent"] == il)
                    is_match = bool(classified and classified.get("intent") == il and classified.get("domain") == dom)
                    prefix = "🎯 " if is_match else ""
                    
                    # Extract utterances for this specific intent
                    utts = [r["utterance"] for r in rows_i if r["domain"] == dom and r["intent"] == il]
                    
                    # Use HTML details tag to avoid nested expander exception
                    utts_html = "".join([f"<li><small>{u}</small></li>" for u in utts])
                    st.markdown(
                        f"""
                        <details {"open" if is_match else ""}>
                            <summary style="cursor: pointer; padding: 2px 0;">
                                {prefix}<code>{il}</code> ({count})
                            </summary>
                            <ul style="list-style-type: none; margin-top: 5px; padding-left: 15px;">
                                {utts_html}
                            </ul>
                        </details>
                        """,
                        unsafe_allow_html=True
                    )


# ---------------------------------------------------------------------------
# Directory tab
# ---------------------------------------------------------------------------
with tab_directory:
    st.subheader("Company Directory")
    st.caption("Full list of indexed companies with metadata.")

    # We use the existing payloads from st.session_state if available, 
    # otherwise fetch them.
    all_data = st.session_state.get("pca_payloads")
    
    if not all_data:
        if st.button("Load Directory"):
            try:
                resp = httpx.get(f"{API_BASE}/vectors", timeout=30)
                resp.raise_for_status()
                raw_points = resp.json()["points"]
                all_data = [p["payload"] for p in raw_points]
                st.session_state.pca_payloads = all_data
                st.rerun()
            except Exception as e:
                st.error(f"Failed to load data: {e}")
    
    if all_data:
        import pandas as pd
        df = pd.DataFrame(all_data)
        
        # Reorder columns for better viewing
        cols = ["company_name", "mapped_industry", "mapped_function", "short_description", "website", "zone", "priority"]
        existing_cols = [c for d in [df.columns] for c in cols if c in d]
        df = df[existing_cols + [c for c in df.columns if c not in existing_cols]]
        
        # Fix type inference for Arrow serialization (prevents 'High' -> int64 errors)
        if "priority" in df.columns:
            df["priority"] = df["priority"].astype(str)
        
        # Search box
        search_term = st.text_input("Filter directory by name or description", "").lower()
        if search_term:
            mask = df.apply(lambda x: x.astype(str).str.lower().str.contains(search_term)).any(axis=1)
            df = df[mask]
            
        st.dataframe(
            df,
            use_container_width=True,
            column_config={
                "website": st.column_config.LinkColumn("Website"),
                "active": st.column_config.CheckboxColumn("Active"),
            },
            hide_index=True,
        )
        st.caption(f"Showing {len(df)} companies")


# ---------------------------------------------------------------------------
# Files tab
# ---------------------------------------------------------------------------
with tab_files:
    st.subheader("Indexed source files")

    col_f1, col_f2 = st.columns([4, 1])
    with col_f1:
        if st.button("Refresh List", type="secondary"):
            st.rerun()
    with col_f2:
        if st.button("🗑️ Clear DB", help="Delete all vectors in the main collection", type="secondary"):
            try:
                resp = httpx.post(f"{API_BASE}/clear-collection", timeout=30)
                resp.raise_for_status()
                st.success("Database cleared!")
                st.rerun()
            except Exception as e:
                st.error(f"Clear failed: {e}")

    try:
        resp = httpx.get(f"{API_BASE}/collections", timeout=10)
        resp.raise_for_status()
        files = resp.json()["files"]
    except Exception as e:
        st.error(f"Could not fetch files: {e}")
        files = []

    if not files:
        st.info("No files indexed yet. Upload one in the Upload tab.")
    else:
        for f in files:
            st.markdown(f"- `{f}`")