File size: 33,927 Bytes
20eb3d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a82cb39
20eb3d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557ce90
 
 
 
 
9c7ff77
6481f5a
 
20eb3d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Streamlit UI for NYC Code Compliance Bot β€” with agent discussion panel."""
from __future__ import annotations

import base64
import hashlib
import hmac
import json
import logging
import os
import tempfile
import time
from pathlib import Path

import pandas as pd
import requests
import streamlit as st
from dotenv import load_dotenv
from huggingface_hub import HfApi, hf_hub_download
from huggingface_hub.utils import EntryNotFoundError
from PIL import Image

from config import MAX_INVESTIGATION_ROUNDS
from graph import compile_compliance_graph
from tools.chroma_tools import warmup_collection, is_warmed_up
from tools.crop_cache import CropCache
from tools.image_store import ImageStore
from tools.metadata_cache import MetadataState, get_cached_metadata
from tools.pdf_processor import render_pages

load_dotenv()

logger = logging.getLogger(__name__)

# =============================================================================
# HF OAuth + Usage Quota Configuration
# =============================================================================

USAGE_DATASET_REPO = "NYSERDA-CRE-Working-Group/nyserda_demo_useage_store"
USAGE_FILENAME = "usage.csv"
MAX_RUNS_PER_USER = 10
HF_TOKEN = os.environ.get("HF_TOKEN")

OAUTH_CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID")
OAUTH_CLIENT_SECRET = os.environ.get("OAUTH_CLIENT_SECRET")
OPENID_PROVIDER_URL = os.environ.get("OPENID_PROVIDER_URL", "https://huggingface.co")
SPACE_HOST = os.environ.get("SPACE_HOST", "")

_hf_api = HfApi(token=HF_TOKEN) if HF_TOKEN else None


# ---------------------------------------------------------------------------
# OAuth helpers
# ---------------------------------------------------------------------------

def _get_oauth_state() -> str:
    """Generate a deterministic OAuth state string for CSRF protection."""
    secret = (OAUTH_CLIENT_SECRET or "fallback-secret").encode()
    return hmac.new(secret, b"hf-oauth-state", hashlib.sha256).hexdigest()[:32]


def _get_redirect_uri() -> str:
    """Build the OAuth redirect URI pointing back to this Space."""
    if SPACE_HOST:
        return f"https://{SPACE_HOST}"
    return "http://localhost:7860"


def _exchange_code_for_user(code: str) -> str | None:
    """Exchange the OAuth authorization code for an access token, then fetch username."""
    if not OAUTH_CLIENT_ID or not OAUTH_CLIENT_SECRET:
        st.error("OAuth is not configured. Set hf_oauth: true in your Space's README.md")
        return None

    redirect_uri = _get_redirect_uri()
    token_url = f"{OPENID_PROVIDER_URL}/oauth/token"
    credentials = base64.b64encode(
        f"{OAUTH_CLIENT_ID}:{OAUTH_CLIENT_SECRET}".encode()
    ).decode()

    try:
        resp = requests.post(
            token_url,
            headers={"Authorization": f"Basic {credentials}"},
            data={
                "grant_type": "authorization_code",
                "code": code,
                "redirect_uri": redirect_uri,
                "client_id": OAUTH_CLIENT_ID,
            },
            timeout=10,
        )
        resp.raise_for_status()
        token_data = resp.json()
    except Exception as e:
        st.error(f"Token exchange failed: {e}")
        return None

    access_token = token_data.get("access_token")
    if not access_token:
        st.error("No access token in OAuth response.")
        return None

    try:
        userinfo_resp = requests.get(
            f"{OPENID_PROVIDER_URL}/oauth/userinfo",
            headers={"Authorization": f"Bearer {access_token}"},
            timeout=10,
        )
        userinfo_resp.raise_for_status()
        userinfo = userinfo_resp.json()
    except Exception as e:
        st.error(f"Failed to fetch user info: {e}")
        return None

    username = userinfo.get("preferred_username") or userinfo.get("sub")
    if username:
        return username.strip().lower()
    return None


def get_hf_user() -> str | None:
    """Check if user is logged in via HF OAuth (authorization code flow)."""
    if "hf_user" in st.session_state:
        return st.session_state["hf_user"]

    params = st.query_params
    code = params.get("code")
    returned_state = params.get("state")

    if code:
        expected_state = _get_oauth_state()
        if returned_state == expected_state:
            user = _exchange_code_for_user(code)
            if user:
                st.session_state["hf_user"] = user
                st.query_params.clear()
                st.rerun()
            else:
                st.error("OAuth login failed β€” could not retrieve user info.")
                st.query_params.clear()
        else:
            st.error("OAuth state mismatch β€” please try signing in again.")
            st.query_params.clear()

    return None


def login_button():
    """Show a 'Sign in with Hugging Face' button that starts the OAuth flow."""
    if not OAUTH_CLIENT_ID:
        st.error(
            "OAuth is not configured for this Space. "
            "Add `hf_oauth: true` to your README.md metadata."
        )
        return

    if st.button("πŸ€— Sign in with Hugging Face", use_container_width=True, type="primary"):
        state = _get_oauth_state()
        redirect_uri = _get_redirect_uri()
        auth_url = (
            f"{OPENID_PROVIDER_URL}/oauth/authorize"
            f"?client_id={OAUTH_CLIENT_ID}"
            f"&redirect_uri={redirect_uri}"
            f"&scope=openid%20profile"
            f"&response_type=code"
            f"&state={state}"
        )
        st.markdown(
            f'<meta http-equiv="refresh" content="0;url={auth_url}">',
            unsafe_allow_html=True,
        )
        st.stop()


# ---------------------------------------------------------------------------
# Usage quota helpers
# ---------------------------------------------------------------------------

def _load_usage_df() -> pd.DataFrame:
    """Load the usage CSV from HF dataset repo."""
    if not _hf_api or not USAGE_DATASET_REPO:
        return pd.DataFrame(columns=["user_id", "runs", "first_seen", "last_seen"])
    try:
        local_path = hf_hub_download(
            repo_id=USAGE_DATASET_REPO,
            repo_type="dataset",
            filename=USAGE_FILENAME,
            token=HF_TOKEN,
        )
        return pd.read_csv(local_path)
    except EntryNotFoundError:
        return pd.DataFrame(columns=["user_id", "runs", "first_seen", "last_seen"])


def _save_usage_df(df: pd.DataFrame, commit_message: str) -> None:
    """Save the usage CSV back to HF dataset repo."""
    if not _hf_api or not USAGE_DATASET_REPO:
        return
    tmp_path = "/tmp/usage.csv"
    df.to_csv(tmp_path, index=False)
    _hf_api.upload_file(
        path_or_fileobj=tmp_path,
        path_in_repo=USAGE_FILENAME,
        repo_id=USAGE_DATASET_REPO,
        repo_type="dataset",
        commit_message=commit_message,
    )


def check_and_increment_quota(user_id: str) -> tuple[bool, int]:
    """Check if user has remaining quota and increment usage.

    Returns (allowed: bool, remaining_runs: int).
    If USAGE_DATASET_REPO is not configured, always allows (unlimited).
    """
    if not USAGE_DATASET_REPO:
        return True, 999

    now = int(time.time())
    df = _load_usage_df()

    if df.empty or (df["user_id"] == user_id).sum() == 0:
        runs = 0
        if runs >= MAX_RUNS_PER_USER:
            return False, 0
        new_row = {
            "user_id": user_id,
            "runs": 1,
            "first_seen": now,
            "last_seen": now,
        }
        df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
        _save_usage_df(df, commit_message=f"usage: increment {user_id} to 1")
        return True, MAX_RUNS_PER_USER - 1

    idx = df.index[df["user_id"] == user_id][0]
    runs = int(df.loc[idx, "runs"])

    if runs >= MAX_RUNS_PER_USER:
        return False, 0

    runs += 1
    df.loc[idx, "runs"] = runs
    df.loc[idx, "last_seen"] = now

    _save_usage_df(df, commit_message=f"usage: increment {user_id} to {runs}")
    return True, MAX_RUNS_PER_USER - runs


# ---------------------------------------------------------------------------
# Page config
# ---------------------------------------------------------------------------
st.set_page_config(
    page_title="NYC Code Compliance Bot",
    page_icon=":building_construction:",
    layout="wide",
)

# ---------------------------------------------------------------------------
# Authentication gate β€” must sign in before using the app
# ---------------------------------------------------------------------------
uid = get_hf_user()
if not uid:
    _left, center, _right = st.columns([1, 2, 1])
    with center:
        st.markdown(
            "<h1 style='text-align: center;'>πŸ—οΈ NYC Code Compliance Bot</h1>",
            unsafe_allow_html=True,
        )
        st.markdown(
            "<p style='text-align: center; color: grey;'>"
            "Sign in with your Hugging Face account to get started."
            "</p>",
            unsafe_allow_html=True,
        )
        login_button()
    st.stop()

# ---------------------------------------------------------------------------
# Custom CSS for agent discussion panel
# ---------------------------------------------------------------------------
st.markdown("""
<style>
.agent-msg {
    padding: 8px 12px;
    margin: 4px 0;
    border-radius: 8px;
    font-size: 0.9em;
}
.agent-planner {
    background-color: #e3f2fd;
    border-left: 4px solid #1565c0;
}
.agent-code_analyst {
    background-color: #fff3e0;
    border-left: 4px solid #e65100;
}
.agent-compliance_analyst {
    background-color: #e8f5e9;
    border-left: 4px solid #2e7d32;
}
.agent-reviewer {
    background-color: #f3e5f5;
    border-left: 4px solid #6a1b9a;
}
.agent-icon {
    font-weight: bold;
    margin-right: 6px;
}
.agent-timestamp {
    color: #666;
    font-size: 0.8em;
}
</style>
""", unsafe_allow_html=True)

AGENT_ICONS = {
    "planner": "\U0001f4cb",
    "code_analyst": "\u2696\ufe0f",
    "compliance_analyst": "\U0001f50d",
    "reviewer": "\U0001f91d",
}

AGENT_LABELS = {
    "planner": "Planner",
    "code_analyst": "Code Analyst",
    "compliance_analyst": "Compliance Analyst",
    "reviewer": "Reviewer",
}

# ---------------------------------------------------------------------------
# Session state defaults
# ---------------------------------------------------------------------------
if "pdf_loaded" not in st.session_state:
    st.session_state.pdf_loaded = False
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []
if "image_store" not in st.session_state:
    st.session_state.image_store = None
if "ingest_state" not in st.session_state:
    st.session_state.ingest_state = {}
if "pdf_bytes" not in st.session_state:
    st.session_state.pdf_bytes = None
if "metadata_state" not in st.session_state:
    st.session_state.metadata_state = MetadataState()
if "crop_cache" not in st.session_state:
    st.session_state.crop_cache = CropCache()
if "discussion_log" not in st.session_state:
    st.session_state.discussion_log = []
if "code_report" not in st.session_state:
    st.session_state.code_report = ""
if "code_sections" not in st.session_state:
    st.session_state.code_sections = []
if "image_refs" not in st.session_state:
    st.session_state.image_refs = []
if "db_ready" not in st.session_state:
    st.session_state.db_ready = False

# ---------------------------------------------------------------------------
# Startup: warm up embedding model + ChromaDB
# ---------------------------------------------------------------------------
if not st.session_state.db_ready:
    with st.status("Loading NYC Code Database...", expanded=True) as _db_status:
        st.write(":brain: Loading embedding model (bge-large-en-v1.5)...")
        st.write("_This is a one-time download (~1.3 GB) on first run._")
        ok = warmup_collection()
        if ok:
            st.session_state.db_ready = True
            _db_status.update(label="NYC Code Database ready", state="complete")
        else:
            _db_status.update(
                label="NYC Code Database not available β€” code lookup will be disabled",
                state="error",
            )
            st.session_state.db_ready = False
    if st.session_state.db_ready:
        st.rerun()

# ---------------------------------------------------------------------------
# Sidebar
# ---------------------------------------------------------------------------
with st.sidebar:
    st.title(":building_construction: NYC Code Compliance Bot")
    st.markdown(
        "Upload a construction drawing PDF and ask compliance questions. "
        "The system uses **agentic vision** + **NYC code database** to "
        "verify code compliance."
    )

    st.divider()

    st.markdown(
        "During business hours Gemini servers see high traffic. "
        "This may slow or crash the application, this is a Gemini issue,"
        "if a response takes more than 1 minute try again later."
    )

    # PDF upload
    uploaded_file = st.file_uploader("Upload Drawing PDF", type=["pdf"])

    # Default drawing button
    _DEFAULT_PDF = Path(__file__).parent / "NorthMaconPark.pdf"
    if _DEFAULT_PDF.exists() and not st.session_state.pdf_loaded:
        st.markdown("**β€” or β€”**")
        if st.button("Use Default Drawing", use_container_width=True):
            st.session_state._use_default_pdf = True
            st.rerun()

    st.divider()

    # Settings
    st.subheader("Settings")
    enable_consensus = st.checkbox(
        "Enable peer review (Gemini + GPT)",
        value=False,
        help="GPT reviews Gemini's compliance analysis. Slower but more thorough.",
    )
    enable_annotation = st.checkbox(
        "Enable annotation",
        value=False,
        help="Annotate crops with numbered highlights before analysis.",
    )
    max_rounds = st.slider(
        "Max investigation rounds",
        min_value=1,
        max_value=5,
        value=MAX_INVESTIGATION_ROUNDS,
        help="Maximum crop-analyze loops before forcing a final verdict.",
    )

    st.divider()

    # --- User info + sign out ---
    st.caption(f"Signed in as: **{uid}**")
    if st.button("Sign out", use_container_width=True):
        st.session_state.pop("hf_user", None)
        st.rerun()

    st.divider()
    st.caption("Powered by LangGraph + Gemini + GPT + ChromaDB")

# ---------------------------------------------------------------------------
# PDF ingestion β€” Phase A: render pages
# ---------------------------------------------------------------------------
_pending_pdf: tuple[str, bytes] | None = None
if not st.session_state.pdf_loaded:
    if uploaded_file is not None:
        _pending_pdf = (uploaded_file.name, uploaded_file.getvalue())
    elif st.session_state.get("_use_default_pdf"):
        _default = Path(__file__).parent / "NorthMaconPark.pdf"
        if _default.exists():
            _pending_pdf = (_default.name, _default.read_bytes())

if _pending_pdf is not None and not st.session_state.pdf_loaded:
    pdf_name, pdf_bytes = _pending_pdf
    with st.status("Converting PDF to images...", expanded=True) as status:
        tmp_dir = tempfile.mkdtemp(prefix="compliance_bot_")
        pdf_path = Path(tmp_dir) / pdf_name
        pdf_path.write_bytes(pdf_bytes)

        st.session_state.pdf_bytes = pdf_bytes
        st.session_state.crop_cache = CropCache()

        image_store = ImageStore(str(Path(tmp_dir) / "images"))
        st.session_state.image_store = image_store
        page_image_dir = str(image_store._pages_dir)

        cached = get_cached_metadata(pdf_bytes)
        if cached is not None:
            st.session_state.metadata_state.set_ready(json.dumps(cached, indent=2))
            st.write("Page index loaded from cache")

        st.write("Rendering pages...")
        num_pages = render_pages(str(pdf_path), page_image_dir)

        st.session_state.ingest_state = {
            "pdf_path": str(pdf_path),
            "page_image_dir": page_image_dir,
            "num_pages": num_pages,
        }
        st.session_state.pdf_loaded = True
        st.session_state.pop("_use_default_pdf", None)
        st.write(f"Converted {num_pages} pages to images.")
        status.update(label=f"PDF ready: {num_pages} pages", state="complete")
    st.rerun()

# ---------------------------------------------------------------------------
# PDF ingestion β€” Phase B: generate page index
# ---------------------------------------------------------------------------
if st.session_state.pdf_loaded:
    meta = st.session_state.metadata_state
    if meta.status == "not_started":
        if st.session_state.pdf_bytes is not None:
            with st.expander(":page_facing_up: PDF Viewer", expanded=False):
                st.pdf(st.session_state.pdf_bytes, height=400)

        ingest = st.session_state.ingest_state
        num_pages = ingest["num_pages"]

        st.write("**Generating page index...**")
        progress_bar = st.progress(0, text="Analyzing pages to build searchable index...")

        def _index_progress(completed: int, total: int, label: str):
            pct = completed / total
            progress_bar.progress(pct, text=f"Indexing: {label}  ({completed}/{total} batches)")

        meta.generate_sync(
            ingest["pdf_path"],
            num_pages,
            st.session_state.pdf_bytes,
            progress_callback=_index_progress,
        )
        if meta.is_ready:
            progress_bar.progress(1.0, text="Page index ready!")
        else:
            progress_bar.progress(1.0, text="Indexing failed β€” using full PDF mode")
        st.rerun()

# ---------------------------------------------------------------------------
# Main layout (pre-upload welcome)
# ---------------------------------------------------------------------------
if not st.session_state.pdf_loaded:
    _left, center, _right = st.columns([1, 2, 1])
    with center:
        st.markdown(
            "<h1 style='text-align: center;'>πŸ—οΈ NYC Code Compliance Bot</h1>",
            unsafe_allow_html=True,
        )
        st.markdown(
            "<p style='text-align: center; color: grey;'>"
            "Upload a construction drawing PDF in the sidebar to get started.<br>"
            "This tool uses <b>agentic vision</b> and the <b>NYC Building Code database</b> "
            "to verify code compliance in your drawings."
            "</p>",
            unsafe_allow_html=True,
        )
    st.stop()

# ---------------------------------------------------------------------------
# PDF viewer
# ---------------------------------------------------------------------------
if st.session_state.pdf_bytes is not None:
    with st.expander(":page_facing_up: PDF Viewer", expanded=False):
        st.pdf(st.session_state.pdf_bytes, height=400)

# ---------------------------------------------------------------------------
# Three-column layout: chat | discussion | images+code
# ---------------------------------------------------------------------------
chat_col, discuss_col, evidence_col = st.columns([2, 2, 2])

# ---------------------------------------------------------------------------
# Discussion panel (agent conversation)
# ---------------------------------------------------------------------------
def render_discussion_log(container, discussion_log: list[dict]):
    """Render the agent discussion log with styled messages."""
    with container:
        for msg in discussion_log:
            agent = msg.get("agent", "unknown")
            icon = AGENT_ICONS.get(agent, "\U0001f916")
            label = AGENT_LABELS.get(agent, agent)
            css_class = f"agent-{agent}"

            st.markdown(
                f'<div class="agent-msg {css_class}">'
                f'<span class="agent-timestamp">[{msg.get("timestamp", "")}]</span> '
                f'<span class="agent-icon">{icon} {label}</span><br>'
                f'{msg.get("summary", "")}'
                f'</div>',
                unsafe_allow_html=True,
            )

# ---------------------------------------------------------------------------
# Chat history display
# ---------------------------------------------------------------------------
with chat_col:
    st.subheader(":speech_balloon: Chat")

    meta = st.session_state.metadata_state
    if meta.is_ready:
        st.caption("Page index ready β€” fast planning enabled")
    elif meta.status == "failed":
        st.caption("Page indexing failed β€” using full PDF mode")

    for role, content, _refs in st.session_state.chat_history:
        with st.chat_message(role):
            st.markdown(content)

    question = st.chat_input("Ask a compliance question about the drawing...")

# ---------------------------------------------------------------------------
# Discussion panel
# ---------------------------------------------------------------------------
with discuss_col:
    st.subheader(":busts_in_silhouette: Agent Discussion")
    discussion_container = st.container()

    if st.session_state.discussion_log:
        render_discussion_log(discussion_container, st.session_state.discussion_log)
    else:
        st.info("Agent discussions will appear here during analysis.")

# ---------------------------------------------------------------------------
# Evidence panel (images + code)
# ---------------------------------------------------------------------------
with evidence_col:
    st.subheader(":framed_picture: Evidence")

    evidence_tabs = st.tabs(["Drawing Crops", "Code Sections"])

    with evidence_tabs[0]:
        if st.session_state.image_refs:
            for ref in st.session_state.image_refs:
                try:
                    img = Image.open(ref["path"])
                    st.image(img, caption=ref["label"], use_container_width=True)
                except Exception:
                    st.warning(f"Could not load: {ref['label']}")
        elif st.session_state.chat_history:
            st.info("No images for this question.")
        else:
            st.info("Ask a question to see drawing crops here.")

    with evidence_tabs[1]:
        if st.session_state.code_sections:
            for sec in st.session_state.code_sections:
                with st.expander(
                    f":balance_scale: {sec.get('code_type', '?')} Β§{sec.get('section_full', '?')}",
                    expanded=False,
                ):
                    if sec.get("relevance"):
                        st.caption(sec["relevance"])
                    st.markdown(sec.get("text", "")[:1500])
            if st.session_state.code_report:
                with st.expander(":page_facing_up: Full Code Report", expanded=False):
                    st.markdown(st.session_state.code_report[:5000])
        else:
            st.info("Code sections retrieved during analysis will appear here.")


# ---------------------------------------------------------------------------
# Question processing
# ---------------------------------------------------------------------------
if question:
    # === QUOTA CHECK ===
    allowed, remaining = check_and_increment_quota(uid)
    if not allowed:
        with chat_col:
            st.error(
                f"Usage limit reached: {MAX_RUNS_PER_USER} queries per user. "
                "Please contact the admin for additional access."
            )
        st.stop()

    if remaining <= 2:
        with chat_col:
            st.warning(f"⚠️ Only {remaining} query(ies) left!")
    # === END QUOTA CHECK ===

    # Add user message to history
    st.session_state.chat_history.append(("user", question, []))
    st.session_state.discussion_log = []
    st.session_state.code_report = ""
    st.session_state.code_sections = []
    st.session_state.image_refs = []

    with chat_col:
        with st.chat_message("user"):
            st.markdown(question)

    # Build initial state
    ingest = st.session_state.ingest_state
    image_store = st.session_state.image_store

    meta = st.session_state.metadata_state
    metadata_json = meta.data_json if meta.is_ready else ""

    question_state = {
        "messages": [],
        "question": question,
        "pdf_path": ingest.get("pdf_path", ""),
        "page_image_dir": ingest.get("page_image_dir", ""),
        "num_pages": ingest.get("num_pages", 0),
        "page_metadata_json": metadata_json,
        "legend_pages": [],
        "target_pages": [],
        "crop_tasks": [],
        "code_queries": [],
        "image_refs": [],
        "code_sections": [],
        "code_report": "",
        "code_chapters_fetched": [],
        "compliance_analysis": "",
        "reviewer_analysis": "",
        "final_verdict": "",
        "discussion_log": [],
        "additional_crop_tasks": [],
        "additional_code_queries": [],
        "needs_more_investigation": False,
        "investigation_round": 0,
        "max_rounds": max_rounds,
        "enable_consensus": enable_consensus,
        "enable_annotation": enable_annotation,
        "status_message": [],
    }

    # ------------------------------------------------------------------
    # Live progress
    # ------------------------------------------------------------------
    crop_cache = st.session_state.crop_cache

    with evidence_col:
        with evidence_tabs[0]:
            crop_counter_placeholder = st.empty()
            crop_image_container = st.container()

    def on_crop_progress(
        completed_ref, crop_task, source: str, completed_count: int, total_count: int,
    ) -> None:
        source_tag = " (cached)" if source == "cached" else ""
        crop_counter_placeholder.markdown(
            f"**Crop {completed_count}/{total_count}**{source_tag}  \n"
            f"Latest: *{crop_task.get('label', 'Crop')}*"
        )
        with crop_image_container:
            try:
                img = Image.open(completed_ref["path"])
                caption = completed_ref["label"]
                if source == "cached":
                    caption += " (cached)"
                st.image(img, caption=caption, use_container_width=True)
            except Exception:
                st.warning(f"Could not load: {completed_ref['label']}")

    # Compile graph
    compliance_graph = compile_compliance_graph(image_store, crop_cache, on_crop_progress)

    # Node progress labels
    PROGRESS_LABELS = {
        "compliance_planner": "Planning investigation...",
        "execute_crops": "Cropping drawing images...",
        "annotate_crops": "Annotating crops...",
        "initial_code_lookup": "Searching NYC code database...",
        "compliance_analyst": "Analyzing compliance...",
        "targeted_code_lookup": "Follow-up code search...",
        "deliberation": "Running peer review...",
        "final_verdict": "Synthesizing verdict...",
    }

    with chat_col:
        with st.status("Investigating compliance...", expanded=True) as status:
            all_image_refs: list[dict] = []
            all_discussion: list[dict] = []
            final_verdict_text = ""
            code_report_text = ""

            st.write(PROGRESS_LABELS["compliance_planner"])

            parallel_status = st.empty()

            for event in compliance_graph.stream(question_state, stream_mode="updates"):
                node_name = list(event.keys())[0]
                update = event[node_name]

                status_msgs = update.get("status_message", [])
                for status_msg in status_msgs:
                    if status_msg:
                        st.write(f":white_check_mark: {status_msg}")

                new_discussion = update.get("discussion_log", [])
                if new_discussion:
                    all_discussion.extend(new_discussion)
                    st.session_state.discussion_log = all_discussion
                    render_discussion_log(discussion_container, all_discussion)

                if node_name == "compliance_planner":
                    target_pages = update.get("target_pages", [])
                    crop_tasks = update.get("crop_tasks", [])
                    code_queries = update.get("code_queries", [])

                    with st.expander(":clipboard: Investigation Plan", expanded=True):
                        if target_pages:
                            st.markdown(f"**Target pages:** {', '.join(str(p + 1) for p in target_pages)}")
                        if crop_tasks:
                            st.markdown(f"**Image crops ({len(crop_tasks)}):**")
                            for i, task in enumerate(crop_tasks, 1):
                                display_page = task.get("page_num", 0) + 1
                                st.markdown(f"  {i}. {task.get('label', 'Crop')} (p.{display_page})")
                        if code_queries:
                            st.markdown(f"**Code queries ({len(code_queries)}):**")
                            for i, q in enumerate(code_queries, 1):
                                st.markdown(f"  {i}. [{q.get('focus_area', '?')}] {q.get('query', '')[:80]}...")

                    if crop_tasks:
                        crop_counter_placeholder.markdown(f"**Crop 0/{len(crop_tasks)}** β€” starting...")

                    parallel_status.info(
                        ":arrows_counterclockwise: Running in parallel: "
                        f"**Cropping {len(crop_tasks)} images** + "
                        f"**Searching {len(code_queries)} code queries**. "
                        "This may take 30-60 seconds..."
                    )

                elif node_name in ("initial_code_lookup", "execute_crops"):
                    parallel_status.empty()

                if node_name in ("initial_code_lookup", "targeted_code_lookup"):
                    report = update.get("code_report", "")
                    new_sections = update.get("code_sections", [])
                    if report:
                        code_report_text = report
                        st.session_state.code_report = report
                    if new_sections:
                        st.session_state.code_sections.extend(new_sections)
                        with evidence_col:
                            with evidence_tabs[1]:
                                for sec in new_sections:
                                    with st.expander(
                                        f":balance_scale: {sec.get('code_type', '?')} "
                                        f"Β§{sec.get('section_full', '?')}",
                                        expanded=False,
                                    ):
                                        if sec.get("relevance"):
                                            st.caption(sec["relevance"])
                                        st.markdown(sec.get("text", "")[:1500])

                elif node_name == "compliance_analyst":
                    analysis = update.get("compliance_analysis", "")
                    needs_more = update.get("needs_more_investigation", False)
                    round_num = update.get("investigation_round", 1)

                    if analysis:
                        label = f":mag: Compliance Analysis (Round {round_num})"
                        if needs_more:
                            label += " β€” requesting more evidence"
                        with st.expander(label, expanded=False):
                            st.markdown(analysis[:5000])

                elif node_name == "deliberation":
                    review = update.get("reviewer_analysis", "")
                    if review:
                        with st.expander(":handshake: Peer Review", expanded=False):
                            st.markdown(review[:3000])

                new_refs = update.get("image_refs", [])
                if new_refs:
                    all_image_refs.extend(new_refs)
                    st.session_state.image_refs.extend(new_refs)
                    with evidence_col:
                        with evidence_tabs[0]:
                            for ref in new_refs:
                                try:
                                    img = Image.open(ref["path"])
                                    st.image(img, caption=ref["label"], use_container_width=True)
                                except Exception:
                                    st.warning(f"Could not load: {ref['label']}")

                if "final_verdict" in update and update["final_verdict"]:
                    final_verdict_text = update["final_verdict"]

                if node_name in PROGRESS_LABELS:
                    next_labels = {
                        "compliance_planner": ["execute_crops", "initial_code_lookup"],
                        "execute_crops": ["compliance_analyst"],
                        "annotate_crops": ["compliance_analyst"],
                        "initial_code_lookup": ["compliance_analyst"],
                        "compliance_analyst": ["final_verdict"],
                        "targeted_code_lookup": ["compliance_analyst"],
                        "deliberation": ["final_verdict"],
                    }
                    for next_node in next_labels.get(node_name, []):
                        if next_node in PROGRESS_LABELS:
                            st.write(PROGRESS_LABELS[next_node])

            if crop_cache.size > 0:
                st.caption(f":file_folder: {crop_cache.stats}")
            status.update(label="Compliance investigation complete", state="complete")

    if final_verdict_text:
        with chat_col:
            with st.chat_message("assistant"):
                st.markdown(final_verdict_text)

        st.session_state.chat_history[-1] = ("user", question, [])
        st.session_state.chat_history.append(("assistant", final_verdict_text, all_image_refs))
    else:
        with chat_col:
            st.error("No verdict was generated. Please try again.")

    st.rerun()