File size: 10,740 Bytes
8b7e8f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from typing import List, Dict
import time

from ..utils.helpers import format_file_size, format_timestamp


def show_library_interface():
    """Display the document library interface."""

    st.header("📚 Document Library")
    st.markdown("Manage and review all your analyzed documents")

    # Get documents from session state
    documents = st.session_state.get("documents_library", [])

    if not documents:
        show_empty_library()
        return

    # Library statistics
    show_library_stats(documents)

    # Filter and search
    show_library_filters(documents)

    # Document grid
    show_document_grid(documents)


def show_empty_library():
    """Show empty library state."""
    st.markdown("---")

    col1, col2, col3 = st.columns([1, 2, 1])

    with col2:
        st.markdown(
            """
        <div style="text-align: center; padding: 3rem;">
            <h3>📚 Your Library is Empty</h3>
            <p style="color: var(--text-color, #666); opacity: 0.7;">Upload and analyze documents to build your personal legal document library.</p>
        </div>
        """,
            unsafe_allow_html=True,
        )

        if st.button(
            "📄 Upload Your First Document", type="primary", use_container_width=True
        ):
            st.session_state.page = "📄 Upload"
            st.rerun()

    # Add sample documents section
    st.markdown("---")
    show_sample_documents_section()


def show_library_stats(documents: List[Dict]):
    """Display library statistics."""
    # Calculate stats
    total_docs = len(documents)
    doc_types = {}
    high_risk_docs = 0

    for doc in documents:
        doc_type = doc.get("document_type", "other")
        doc_types[doc_type] = doc_types.get(doc_type, 0) + 1

        if doc.get("risk_score", 0) > 60:
            high_risk_docs += 1

    # Display stats
    col1, col2, col3, col4 = st.columns(4)

    with col1:
        st.metric(label="Total Documents", value=total_docs)

    with col2:
        most_common_type = max(doc_types, key=doc_types.get) if doc_types else "None"
        st.metric(label="Most Common Type", value=most_common_type.title())

    with col3:
        st.metric(
            label="High Risk Documents",
            value=high_risk_docs,
            delta=(
                f"{high_risk_docs/total_docs*100:.0f}% of total"
                if total_docs > 0
                else "0%"
            ),
        )

    with col4:
        total_size = sum(doc.get("file_size", 0) for doc in documents)
        st.metric(label="Total Storage", value=format_file_size(total_size))


def show_library_filters(documents: List[Dict]):
    """Display filter and search options."""
    st.markdown("---")

    col1, col2, col3 = st.columns(3)

    with col1:
        # Document type filter
        doc_types = ["All"] + list(
            set(doc.get("document_type", "other") for doc in documents)
        )
        selected_type = st.selectbox("Filter by Type", doc_types)

    with col2:
        # Risk level filter
        risk_levels = [
            "All",
            "Low Risk (0-30)",
            "Medium Risk (31-60)",
            "High Risk (61+)",
        ]
        selected_risk = st.selectbox("Filter by Risk", risk_levels)

    with col3:
        # Search
        search_term = st.text_input(
            "Search documents", placeholder="Enter filename or content..."
        )

    # Apply filters
    filtered_docs = documents

    if selected_type != "All":
        filtered_docs = [
            doc for doc in filtered_docs if doc.get("document_type") == selected_type
        ]

    if selected_risk != "All":
        if "Low Risk" in selected_risk:
            filtered_docs = [
                doc for doc in filtered_docs if doc.get("risk_score", 0) <= 30
            ]
        elif "Medium Risk" in selected_risk:
            filtered_docs = [
                doc for doc in filtered_docs if 31 <= doc.get("risk_score", 0) <= 60
            ]
        elif "High Risk" in selected_risk:
            filtered_docs = [
                doc for doc in filtered_docs if doc.get("risk_score", 0) > 60
            ]

    if search_term:
        filtered_docs = [
            doc
            for doc in filtered_docs
            if search_term.lower() in doc.get("filename", "").lower()
        ]

    # Store filtered docs for grid display
    st.session_state.filtered_documents = filtered_docs


def show_document_grid(documents: List[Dict]):
    """Display documents in a grid layout."""
    filtered_docs = st.session_state.get("filtered_documents", documents)

    if not filtered_docs:
        st.info("No documents match your filter criteria.")
        return

    st.markdown("---")
    st.subheader(f"📄 Documents ({len(filtered_docs)})")

    # Display documents in cards
    for i in range(0, len(filtered_docs), 2):
        col1, col2 = st.columns(2)

        # First document
        with col1:
            if i < len(filtered_docs):
                show_document_card(filtered_docs[i])

        # Second document
        with col2:
            if i + 1 < len(filtered_docs):
                show_document_card(filtered_docs[i + 1])


def show_document_card(doc: Dict):
    """Display a single document card."""
    # Risk color
    risk_score = doc.get("risk_score", 0)
    if risk_score > 60:
        risk_color = "🔴"
        risk_label = "High Risk"
    elif risk_score > 30:
        risk_color = "🟠"
        risk_label = "Medium Risk"
    else:
        risk_color = "🟢"
        risk_label = "Low Risk"

    # Use container for card styling
    with st.container():
        # Header row with filename and risk
        col1, col2 = st.columns([3, 1])
        with col1:
            st.markdown(f"**📄 {doc.get('filename', 'Unknown')}**")
        with col2:
            st.markdown(f"{risk_color} {risk_label}")

        # Document details
        doc_type = doc.get("document_type", "other").title()
        upload_date = doc.get("upload_date", "Unknown")
        file_size = format_file_size(doc.get("file_size", 0))

        st.markdown(f"📋 {doc_type} • 📅 {upload_date} • 💾 {file_size}")

        # Add some spacing
        st.markdown("---")

    # Action buttons
    col1, col2, col3 = st.columns(3)

    with col1:
        if st.button("📊 View", key=f"view_{doc['id']}", use_container_width=True):
            load_document_for_analysis(doc["id"])

    with col2:
        if st.button("💬 Q&A", key=f"qa_{doc['id']}", use_container_width=True):
            load_document_for_qa(doc["id"])

    with col3:
        if st.button("🗑️ Delete", key=f"delete_{doc['id']}", use_container_width=True):
            delete_document(doc["id"])


def load_document_for_analysis(doc_id: str):
    """Load a document from library for analysis."""
    documents = st.session_state.get("documents_library", [])

    for doc in documents:
        if doc["id"] == doc_id:
            # Simulate loading the full document data
            st.session_state.current_document = {
                "id": doc["id"],
                "filename": doc["filename"],
                "document_type": doc["document_type"],
                "original_text": f"Sample content for {doc['filename']}...",  # In real app, load from storage
                "is_sample": True,  # Mark as sample for demo
                "risk_score": doc.get("risk_score", 0),
            }

            st.session_state.page = "📊 Analysis"
            st.rerun()
            break


def load_document_for_qa(doc_id: str):
    """Load a document from library for Q&A."""
    documents = st.session_state.get("documents_library", [])

    for doc in documents:
        if doc["id"] == doc_id:
            # Simulate loading the full document data
            st.session_state.current_document = {
                "id": doc["id"],
                "filename": doc["filename"],
                "document_type": doc["document_type"],
                "original_text": f"Sample content for {doc['filename']}...",  # In real app, load from storage
                "is_sample": True,  # Mark as sample for demo
            }

            st.session_state.page = "💬 Q&A"
            st.rerun()
            break


def delete_document(doc_id: str):
    """Delete a document from the library."""
    # Confirm deletion
    if st.session_state.get(f"confirm_delete_{doc_id}"):
        documents = st.session_state.get("documents_library", [])
        st.session_state.documents_library = [
            doc for doc in documents if doc["id"] != doc_id
        ]

        # Clear confirmation state
        del st.session_state[f"confirm_delete_{doc_id}"]

        st.success("✅ Document deleted from library")


def show_sample_documents_section():
    """Show available sample documents for testing."""
    import os

    st.subheader("🎯 Try Sample Documents")
    st.markdown("Get started by analyzing our sample legal documents:")

    # Get available sample documents
    sample_dir = "./sample"
    sample_files = []
    if os.path.exists(sample_dir):
        sample_files = [
            f for f in os.listdir(sample_dir) if f.endswith((".pdf", ".docx", ".txt"))
        ]

    if sample_files:
        # Create description mapping for better UX
        descriptions = {
            "Employment_Offer_Letter.pdf": "📋 Analyze employment terms, benefits, and obligations",
            "Master_Services_Agreement.pdf": "🤝 Review service agreements and contract terms",
            "Mutual_NDA.pdf": "🔒 Examine confidentiality and non-disclosure clauses",
            "Residential_Lease_Agreement.pdf": "🏠 Check rental terms, deposits, and tenant rights",
        }

        for filename in sample_files:
            with st.expander(
                f"📄 {filename.replace('_', ' ').replace('.pdf', '')}", expanded=False
            ):
                col1, col2 = st.columns([2, 1])

                with col1:
                    description = descriptions.get(
                        filename, "📊 Analyze this legal document for risks and terms"
                    )
                    st.markdown(description)

                with col2:
                    if st.button(
                        "Analyze Now",
                        key=f"sample_lib_{filename}",
                        use_container_width=True,
                    ):
                        # Set this as the sample to load and redirect to upload page
                        st.session_state.load_sample = filename
                        st.session_state.page = "📄 Upload"
                        st.rerun()
    else:
        st.info("No sample documents available.")