File size: 24,354 Bytes
76c1245
d39e016
27f3a96
71ba388
 
c0500a1
931e3ea
 
7794b95
03e1db1
ff10369
 
 
79b9650
 
ff10369
 
 
28723a4
 
f939786
28723a4
 
 
82ea64e
6d02fb7
 
28723a4
 
 
 
6d02fb7
 
4411454
75a237f
 
 
6d02fb7
 
28723a4
 
75a237f
 
 
 
 
 
 
 
 
 
4fb39db
7fcb277
4fb39db
065bacc
76c1245
d1f5e00
 
 
 
 
 
 
 
b258cb7
 
 
 
 
 
 
 
 
4411454
 
 
 
 
 
01fd862
 
 
 
 
 
 
 
 
4411454
2839e79
01fd862
2839e79
01fd862
2839e79
01fd862
 
4d44057
01fd862
4411454
 
01fd862
 
4411454
c350675
5e2a2f9
065bacc
5e2a2f9
 
 
 
065bacc
5e2a2f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4411454
 
 
065bacc
5e2a2f9
 
 
4411454
 
5e2a2f9
065bacc
 
 
 
 
fffae44
 
 
 
 
 
 
b258cb7
fffae44
 
46c7a83
fffae44
 
 
46c7a83
 
fffae44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b258cb7
065bacc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76c1245
5883a13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1b81dc
b258cb7
786a572
b258cb7
786a572
 
 
 
 
 
b258cb7
786a572
 
 
 
 
 
 
 
 
 
 
 
 
 
b258cb7
 
786a572
 
b258cb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
786a572
 
 
b258cb7
 
 
 
065bacc
fffae44
25c34ad
fffae44
 
 
 
 
 
 
15e61e4
fffae44
 
 
 
9bc3aef
 
fffae44
 
 
 
 
 
 
 
 
15e61e4
fffae44
 
 
 
 
 
 
 
 
 
 
 
065bacc
 
 
 
 
92a5f2a
 
25c34ad
065bacc
 
92a5f2a
065bacc
 
 
 
 
 
 
25c34ad
065bacc
92a5f2a
065bacc
 
 
d39e016
67f2631
d39e016
 
94dc3dd
67f2631
 
 
 
d39e016
94dc3dd
d39e016
 
 
 
 
94dc3dd
d39e016
 
 
 
 
 
67f2631
 
 
 
 
 
 
 
 
 
 
 
 
 
94dc3dd
 
d39e016
 
 
 
 
 
 
 
 
 
 
 
67f2631
 
 
 
 
 
 
 
 
 
 
 
 
94dc3dd
d39e016
67f2631
94dc3dd
 
cc5e0da
67f2631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc5e0da
de01648
ed40d54
 
 
 
 
 
 
94dc3dd
15e61e4
b258cb7
 
 
 
065bacc
15e61e4
b258cb7
 
 
ed40d54
 
 
 
b258cb7
 
ed40d54
 
 
15e61e4
ed40d54
b258cb7
03e1db1
b258cb7
 
 
 
ed40d54
b258cb7
 
 
 
03e1db1
b258cb7
03e1db1
 
ed40d54
03e1db1
 
 
 
 
ed40d54
03e1db1
ed40d54
 
03e1db1
ed40d54
 
 
 
 
 
03e1db1
 
 
 
 
 
 
 
 
 
ed40d54
 
 
03e1db1
 
ed40d54
03e1db1
b258cb7
 
ed40d54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc5e0da
c3ceb0f
d39e016
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
# app.py

import streamlit as st
import os
from pathlib import Path
from typing import Dict, List, Optional, Any
from datetime import datetime
from threading import Lock
from dataclasses import dataclass
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder  # MessagesPlaceholder from here
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage  # Remove MessagesPlaceholder from here
from langchain.chains import ConversationalRetrievalChain
from langchain_core.runnables import RunnablePassthrough
from langchain.memory import ConversationBufferMemory

from utils.database import (
    # Base database operations
    create_connection,
    create_tables,
    get_all_documents,
    force_recreate_collections_tables,
    
    # Collection operations
    get_collections,
    create_collection,
    add_document_to_collection,
    remove_from_collection,
    update_collection,
    get_collection_documents,
    handle_document_upload,
    verify_database_tables,
    initialize_qa_system,
    get_embeddings_model,
    # Search functionality
    search_documents
)

# Component imports
from components.chat import display_chat_interface
from components.collection_manager import (
    display_enhanced_collections,
    show_collection_creation_dialog
)

# Optional analytics import if you're using it
from utils.analytics import display_analytics_dashboard

from components.chat import display_chat_interface
from components.document_store import display_documents_tab

# Create locks for thread-safe operations
conn_lock = Lock()
if not os.path.exists('/data'):
    try:
        from setup import setup_directories
        if not setup_directories():
            raise Exception("Failed to set up directories")
    except Exception as e:
        st.error(f"Setup error: {e}")
        st.stop()
        
def handle_chat_state_change():
    """Handle changes in chat state when switching tabs or collections."""
    if st.session_state.get('chat_ready'):
        # Check if we need to reinitialize with new documents
        if st.session_state.get('reinitialize_chat'):
            initialize_chat_from_collection()
            st.session_state.reinitialize_chat = False
            
def verify_database_tables(conn):
    """Verify that all required tables exist."""
    try:
        cursor = conn.cursor()
        # Get list of all tables
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = [table[0] for table in cursor.fetchall()]
        
        # Check for required tables
        required_tables = {
            'documents', 'queries', 'annotations', 
            'collections', 'document_collections', 'chats', 'chat_messages'
        }
        
        missing_tables = required_tables - set(tables)
        
        # If collections table doesn't exist, force recreate it
        if 'collections' not in tables:
            if force_recreate_collections_tables(conn):
                st.session_state.show_debug = True
            else:
                st.error("Failed to create required database tables.")
                return False
            
        return True
            
    except Exception as e:
        st.error(f"Error verifying database tables: {e}")
        return False

def initialize_database():
    """Initialize database with persistent storage."""
    try:
        if 'db_conn' not in st.session_state:
            # Get storage path
            if os.path.exists('/data'):
                base_path = Path('/data')
            else:
                base_path = Path(os.getcwd()) / 'data'
                
            # Ensure data directory exists
            base_path.mkdir(parents=True, exist_ok=True)
            
            # Create database path
            db_path = base_path / 'rfp_analysis.db'
            
            try:
                # Ensure file can be created
                db_path.touch(exist_ok=True)
            except Exception as e:
                st.error(f"Failed to create database file: {e}")
                return False
            
            # Create connection
            conn = create_connection(str(db_path))
            if conn is not None:
                # Create all tables (includes collection tables)
                create_tables(conn)
                st.session_state.db_conn = conn
                
                # Verify tables were created
                verify_database_tables(conn)
                return True
            else:
                return False
        else:
            # Verify tables exist in existing connection
            verify_database_tables(st.session_state.db_conn)
            return True

    except Exception as e:
        st.error(f"Database initialization error: {e}")
        return False

@dataclass
class SessionStateDefaults:
    """Default values for session state variables."""
    # Collection Management
    show_collection_dialog: bool = False
    selected_collection: Optional[Dict] = None
    current_collection: Optional[Dict] = None
    
    # Chat System
    chat_ready: bool = False
    messages: Optional[List] = None  # Changed from list[BaseMessage]
    current_chat_id: Optional[int] = None
    
    # Document Processing
    vector_store: Optional[Any] = None
    qa_system: Optional[Any] = None
    
    # State Management
    reinitialize_chat: bool = False
    debug_mode: bool = False

def initialize_session_state() -> None:
    """
    Initialize all session state variables with proper typing and documentation.
    """
    defaults = SessionStateDefaults()
    
    # Dictionary mapping state variables to their descriptions
    state_vars: Dict[str, Dict[str, Any]] = {
        'show_collection_dialog': {
            'default': defaults.show_collection_dialog,
            'desc': 'Controls visibility of collection creation dialog'
        },
        'selected_collection': {
            'default': defaults.selected_collection,
            'desc': 'Currently selected collection for viewing/editing'
        },
        'current_collection': {
            'default': defaults.current_collection,
            'desc': 'Active collection for chat/analysis'
        },
        'chat_ready': {
            'default': defaults.chat_ready,
            'desc': 'Indicates if chat system is initialized and ready'
        },
        'messages': {
            'default': [] if defaults.messages is None else defaults.messages,
            'desc': 'List of chat messages in the current session'
        },
        'current_chat_id': {
            'default': defaults.current_chat_id,
            'desc': 'ID of the current chat session'
        },
        'vector_store': {
            'default': defaults.vector_store,
            'desc': 'FAISS vector store for document embeddings'
        },
        'qa_system': {
            'default': defaults.qa_system,
            'desc': 'Initialized QA system for chat'
        },
        'reinitialize_chat': {
            'default': defaults.reinitialize_chat,
            'desc': 'Flag to trigger chat system reinitialization'
        },
        'debug_mode': {
            'default': defaults.debug_mode,
            'desc': 'Enable/disable debug information'
        }
    }
    
    # Initialize each state variable if not already present
    for var_name, config in state_vars.items():
        if var_name not in st.session_state:
            st.session_state[var_name] = config['default']

def reset_chat_state() -> None:
    """Reset chat-related session state variables to defaults."""
    st.session_state.messages = []
    st.session_state.current_chat_id = None
    st.session_state.chat_ready = False
    st.session_state.qa_system = None

def reset_collection_state() -> None:
    """Reset collection-related session state variables to defaults."""
    st.session_state.selected_collection = None
    st.session_state.current_collection = None
    st.session_state.show_collection_dialog = False

def get_current_state() -> Dict[str, Any]:
    """
    Get the current state of all session variables.
    Useful for debugging and state management.
    """
    return {
        key: value for key, value in st.session_state.items()
        if not key.startswith('_')  # Exclude internal Streamlit states
    }

def display_top_bar():
    """Display the application's top navigation bar."""
    col1, col2, col3 = st.columns([1, 3, 1])
    
    with col1:
        if os.path.exists("img/logo.png"):
            st.image("img/logo.png", width=100)
        else:
            st.warning("Logo not found at img/logo.png")
    
    with col2:
        st.title("Synaptyx.AI - RFP Analysis Agent")
    
    with col3:
        if st.session_state.current_collection:
            st.info(f"πŸ“ Active Collection: {st.session_state.current_collection['name']}")

def initialize_chat_from_existing():
    """Initialize chat system from existing documents in database."""
    if not st.session_state.get('chat_ready'):
        try:
            documents = get_all_documents(st.session_state.db_conn)
            if documents:
                # Initialize vector store and QA system with existing documents
                embeddings = get_embeddings_model()
                chunks = []
                for doc in documents:
                    doc_chunks = text_splitter.split_text(doc['content'])
                    for chunk in doc_chunks:
                        chunks.append({
                            'content': chunk,
                            'metadata': {'source': doc['name'], 'document_id': doc['id']}
                        })
                
                vector_store = FAISS.from_texts(
                    [chunk['content'] for chunk in chunks],
                    embeddings,
                    [chunk['metadata'] for chunk in chunks]
                )
                
                st.session_state.vector_store = vector_store
                st.session_state.qa_system = initialize_qa_system(vector_store)
                st.session_state.chat_ready = True
                return True
        except Exception as e:
            st.error(f"Error initializing chat: {e}")
    return False

def initialize_chat_from_collection():
    """Initialize chat system with vector store reuse."""
    try:
        documents = None
        if st.session_state.get('current_collection'):
            documents = get_collection_documents(st.session_state.db_conn, 
                                              st.session_state.current_collection['id'])
        else:
            documents = get_all_documents(st.session_state.db_conn)

        if documents:
            document_ids = [doc['id'] for doc in documents]
            
            # Check for existing vector store
            vector_store = get_existing_vector_store(document_ids)
            
            if vector_store:
                # Reuse existing vector store
                st.session_state.vector_store = vector_store
                st.session_state.qa_system = initialize_qa_system(vector_store)
                return True
            
            # If no existing vector store, create new one
            with st.spinner("Initializing chat system..."):
                embeddings = get_embeddings_model()
                text_splitter = RecursiveCharacterTextSplitter(
                    chunk_size=500,
                    chunk_overlap=50,
                    length_function=len,
                    separators=["\n\n", "\n", " ", ""]
                )
                
                chunks = []
                for doc in documents:
                    doc_chunks = text_splitter.split_text(doc['content'])
                    for chunk in doc_chunks:
                        chunks.append({
                            'content': chunk,
                            'metadata': {'source': doc['name'], 'document_id': doc['id']}
                        })
                
                vector_store = FAISS.from_texts(
                    [chunk['content'] for chunk in chunks],
                    embeddings,
                    [chunk['metadata'] for chunk in chunks]
                )
                
                st.session_state.vector_store = vector_store
                st.session_state.qa_system = initialize_qa_system(vector_store)
                return True
                
        return False
        
    except Exception as e:
        st.error(f"Error initializing chat: {e}")
        return False

def display_collection_sidebar():
    """Display enhanced sidebar with collection management."""
    with st.sidebar:
        st.title("πŸ’¬ Chat Controls")
        
        if st.button("πŸ”„ Start New Chat", use_container_width=True):
            st.session_state.messages = []
            st.session_state.current_chat_id = None
            if st.session_state.chat_ready:
                st.rerun()
        
        st.divider()
        
        # Collection Management
        st.title("πŸ“š Collections")
        collections = get_collections(st.session_state.db_conn)
        
        if st.button("βž• Create Collection", use_container_width=True):
            st.session_state.show_collection_dialog = True
        
        # Collection selection with document preview
        if collections:
            selected = st.selectbox(
                "Select Collection",
                options=["All Documents"] + [c['name'] for c in collections],
                key="collection_select"
            )
            
            if selected != "All Documents":
                collection = next((c for c in collections if c['name'] == selected), None)
                if collection:
                    documents = get_collection_documents(st.session_state.db_conn, collection['id'])
                    if documents:
                        st.markdown("### Documents in Collection")
                        for doc in documents:
                            with st.expander(f"πŸ“„ {doc['name']}", expanded=False):
                                st.caption(f"Uploaded: {doc['upload_date']}")
                                if st.button("Use for Chat", key=f"use_{doc['id']}"):
                                    initialize_chat_for_document(doc['id'])

def display_collection_dialog():
    """Display the create collection dialog."""
    with st.sidebar:
        st.subheader("Create New Collection")
        name = st.text_input("Collection Name", key="sidebar_collection_name")
        description = st.text_area("Description", key="sidebar_collection_desc")
        
        col1, col2 = st.columns(2)
        with col1:
            if st.button("Create", key="sidebar_create_btn"):
                if name:
                    if create_collection(st.session_state.db_conn, name, description):
                        st.success(f"Collection '{name}' created!")
                        st.session_state.show_collection_dialog = False
                        st.rerun()
                else:
                    st.error("Please enter a collection name")
        
        with col2:
            if st.button("Cancel", key="sidebar_cancel_btn"):
                st.session_state.show_collection_dialog = False
                st.rerun()

def display_welcome_screen():
    """Display the welcome screen with getting started information and enhanced features."""
    st.title("πŸ€– Welcome to SYNAPTYX")
    st.markdown("### Your AI-powered RFP Analysis Assistant")
    
    # Check for existing documents
    documents = get_all_documents(st.session_state.db_conn)
    collections = get_collections(st.session_state.db_conn)
    
    col1, col2 = st.columns(2)
    with col1:
        st.markdown("""
        #### Getting Started:
        1. Upload your RFP documents using the sidebar
        2. Organize documents into collections
        3. Start analyzing with AI!
        
        You can:
        - Create multiple collections
        - Upload documents to specific collections
        - Search across all documents
        - Get AI-powered insights and summaries
        """)
        
        # Add action buttons if documents exist
        if documents:
            st.success(f"πŸŽ‰ You have {len(documents)} documents ready for analysis!")
            col_a, col_b = st.columns(2)
            with col_a:
                if st.button("Start Chatting", use_container_width=True):
                    if initialize_chat_from_collection():
                        st.session_state.chat_ready = True
                        st.rerun()
            with col_b:
                if st.button("View Documents", use_container_width=True):
                    st.session_state.show_document_store = True
                    st.rerun()
    
    with col2:
        st.markdown("#### Example Questions:")
        examples = [
            "πŸ“Š Summarize the main points of the document",
            "πŸ“ Draft a 'Why Us' section based on the document",
            "🎯 Extract key success metrics and outcomes",
            "πŸ’‘ What are the innovative solutions mentioned?",
            "🀝 Analyze the partnership benefits described",
            "πŸ“ˆ Compare requirements across documents",
            "πŸ” Find similar sections across RFPs"
        ]
        for example in examples:
            st.markdown(f"β€’ {example}")
        
        # Add collection stats if they exist
        if collections:
            st.markdown("---")
            st.markdown("#### Your Collections:")
            for collection in collections[:3]:  # Show top 3 collections
                with st.container():
                    st.markdown(f"""
                    πŸ—‚οΈ **{collection['name']}**  
                    Documents: {collection['doc_count']}
                    """)
            if len(collections) > 3:
                st.caption("... and more collections")

def display_chat_area():
    """Display the main chat interface or welcome screen with proper state management."""
    if not st.session_state.chat_ready:
        display_welcome_screen()
    else:
        col1, col2 = st.columns([3, 1])
        with col1:
            display_chat_interface()
        with col2:
            # Document context panel
            st.markdown("### Current Context")
            if st.session_state.current_collection:
                st.info(f"πŸ“ Using Collection: {st.session_state.current_collection['name']}")
            else:
                st.info("πŸ“š Using All Documents")
            
            # Quick actions
            st.markdown("### Quick Actions")
            if st.button("πŸ”„ New Chat", use_container_width=True):
                reset_chat_state()
                st.rerun()
            if st.button("πŸ“‹ Export Chat", use_container_width=True):
                export_chat_history()
            
            # Example questions for quick reference
            with st.expander("πŸ’‘ Question Examples", expanded=False):
                examples = [
                    "Summarize main points",
                    "Extract requirements",
                    "Compare solutions",
                    "Analyze pricing"
                ]
                for example in examples:
                    if st.button(example, key=f"example_{example}"):
                        # Set the example as the current question
                        st.session_state.current_question = example
                        st.rerun()

def export_chat_history():
    """Export current chat history."""
    if st.session_state.messages:
        chat_text = "\n\n".join([
            f"{'User' if isinstance(m, HumanMessage) else 'Assistant'}: {m.content}"
            for m in st.session_state.messages
        ])
        st.download_button(
            "Download Chat History",
            chat_text,
            file_name=f"chat_export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt",
            mime="text/plain"
        )

def main():
    """Main application function."""
    # Page configuration
    st.set_page_config(
        layout="wide",
        page_title="SYNAPTYX - RFP Analysis Agent",
        initial_sidebar_state="expanded"
    )
    
    # Initialize database and session state
    if not initialize_database():
        st.error("Failed to initialize database. Please contact support.")
        return

    initialize_session_state()
    
    # Top bar with larger logo
    col1, col2, col3 = st.columns([1.5, 4, 1])
    with col1:
        if os.path.exists("img/logo.png"):
            st.image("img/logo.png", width=200)  # Increased logo size
        else:
            st.info("Logo not found")
    with col2:
        st.title("SYNAPTYX - RFP Analysis Agent")
    with col3:
        if st.session_state.current_collection:
            st.info(f"πŸ“ Active Collection: {st.session_state.current_collection['name']}")
    
    # Sidebar
    with st.sidebar:
        # Chat Controls Section
        st.title("πŸ’¬ Chat Controls")
        
        # New Chat button
        if st.button("πŸ”„ Start New Chat", use_container_width=True):
            reset_chat_state()
            st.rerun()
        
        st.divider()
        
        # Document Manager Section
        st.title("πŸ“š Document Manager")
        
        # Collection Creation Button
        if st.button("βž• Create New Collection", use_container_width=True):
            st.session_state.show_collection_dialog = True

        # Collection Selection
        collections = get_collections(st.session_state.db_conn)
        if collections:
            selected = st.selectbox(
                "Select Collection",
                options=["All Documents"] + [c['name'] for c in collections],
                key="collection_select"
            )
            
            if selected != "All Documents":
                collection = next((c for c in collections if c['name'] == selected), None)
                if collection:
                    st.session_state.current_collection = collection
                    display_collection_documents(collection['id'])
        
        # Upload Section
        st.header("Upload Documents")
        uploaded_files = st.file_uploader(
            "Upload PDF documents",
            type=['pdf'],
            accept_multiple_files=True,
            help="Limit 200MB per file β€’ PDF"
        )

        if uploaded_files:
            handle_document_upload(uploaded_files, 
                                 collection_id=st.session_state.current_collection.get('id') if st.session_state.current_collection else None)
    
    # Show collection creation dialog if triggered
    if st.session_state.show_collection_dialog:
        show_collection_creation_dialog()
    
    # Main content area
    if st.session_state.chat_ready:
        display_chat_interface()
    else:
        display_welcome_screen()

    # Debug mode (if enabled)
    if st.session_state.debug_mode and st.sidebar.checkbox("Show Debug Info"):
        st.sidebar.write("Current State:", get_current_state())

def display_collection_documents(collection_id: int):
    """Display documents in the selected collection."""
    documents = get_collection_documents(st.session_state.db_conn, collection_id)
    if documents:
        st.markdown("### Documents in Collection")
        for doc in documents:
            with st.expander(f"πŸ“„ {doc['name']}", expanded=False):
                st.caption(f"Uploaded: {doc['upload_date']}")
                col1, col2 = st.columns(2)
                with col1:
                    if st.button("Preview", key=f"preview_{doc['id']}"):
                        st.text_area(
                            "Content Preview",
                            value=doc['content'][:500] + "..." if len(doc['content']) > 500 else doc['content'],
                            height=100,
                            disabled=True
                        )
                with col2:
                    if st.button("Use for Chat", key=f"chat_{doc['id']}"):
                        initialize_chat_from_collection()
                        st.rerun()

if __name__ == "__main__":
    main()