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
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
"""
Document Preview & Download Manager
Handles displaying generated documents with preview and download capabilities
"""

import os
from typing import Dict, List, Tuple, Optional, Any
from pathlib import Path
import base64
import json
import logging

logger = logging.getLogger(__name__)


class DocumentPreviewManager:
    """
    Manage document preview and download for generated documents.
    """

    def __init__(self, output_dir: str = "generated_documents"):
        """
        Initialize preview manager.

        Args:
            output_dir: Directory to store generated documents
        """
        self.output_dir = output_dir
        os.makedirs(output_dir, exist_ok=True)
        self.documents_registry: Dict[str, Dict[str, Any]] = {}

    def register_document(
        self,
        title: str,
        file_paths: Dict[str, str],
        content_preview: str = "",
        metadata: Dict[str, Any] = None
    ) -> str:
        """
        Register a generated document for preview/download.

        Args:
            title: Document title
            file_paths: Dict of format -> file_path (e.g., {"PDF": "/path/to/file.pdf"})
            content_preview: Text preview of content
            metadata: Additional metadata

        Returns:
            Document ID
        """
        import uuid
        from datetime import datetime

        doc_id = str(uuid.uuid4())[:8]

        self.documents_registry[doc_id] = {
            "title": title,
            "file_paths": file_paths,
            "content_preview": content_preview,
            "created_at": datetime.now().isoformat(),
            "metadata": metadata or {},
            "access_count": 0
        }

        logger.info(f"Document registered: {doc_id} - {title}")
        return doc_id

    def get_document_info(self, doc_id: str) -> Optional[Dict[str, Any]]:
        """Get information about a registered document."""
        if doc_id in self.documents_registry:
            self.documents_registry[doc_id]["access_count"] += 1
            return self.documents_registry[doc_id]
        return None

    def get_download_link(self, doc_id: str, format_type: str) -> Optional[str]:
        """
        Get download link for a specific format.

        Args:
            doc_id: Document ID
            format_type: Format (PDF, Word, Markdown, HTML)

        Returns:
            File path or None
        """
        doc_info = self.get_document_info(doc_id)
        if doc_info:
            return doc_info["file_paths"].get(format_type.upper())
        return None

    def get_preview_html(self, doc_id: str) -> str:
        """
        Get HTML preview of document.

        Args:
            doc_id: Document ID

        Returns:
            HTML content for preview
        """
        doc_info = self.get_document_info(doc_id)
        if not doc_info:
            return "<p>Document not found</p>"

        title = doc_info["title"]
        preview = doc_info["content_preview"]
        formats = list(doc_info["file_paths"].keys())

        # Generate HTML with download buttons
        html = f"""
        <div style="padding: 20px; background: #f8f9fa; border-radius: 8px;">
            <h2>{title}</h2>
            <p>{preview[:500]}...</p>
            <div style="margin-top: 15px; display: flex; gap: 10px; flex-wrap: wrap;">
        """

        for fmt in formats:
            html += f'<button style="padding: 8px 16px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">{fmt}</button>'

        html += """
            </div>
        </div>
        """

        return html

    def get_formats_available(self, doc_id: str) -> List[str]:
        """Get available formats for a document."""
        doc_info = self.get_document_info(doc_id)
        if doc_info:
            return list(doc_info["file_paths"].keys())
        return []

    def get_all_documents(self) -> Dict[str, Dict[str, Any]]:
        """Get all registered documents."""
        return self.documents_registry.copy()

    def generate_download_interface(self, doc_id: str) -> str:
        """
        Generate a user-friendly download interface string.

        Args:
            doc_id: Document ID

        Returns:
            Formatted string with download options
        """
        doc_info = self.get_document_info(doc_id)
        if not doc_info:
            return "❌ Document not found"

        title = doc_info["title"]
        file_paths = doc_info["file_paths"]

        result = f"""
╔═══════════════════════════════════════════════════╗
β•‘           πŸ“„ DOCUMENT READY FOR DOWNLOAD          β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

πŸ“‹ Document: {title}
πŸ†” Document ID: {doc_id}

πŸ“₯ AVAILABLE FORMATS:
"""

        for fmt, path in file_paths.items():
            if os.path.exists(path):
                size_mb = os.path.getsize(path) / (1024 * 1024)
                result += f"  βœ“ {fmt:12} - {size_mb:.2f} MB  [Click to download]\n"
            else:
                result += f"  βœ— {fmt:12} - File not found\n"

        result += f"""
πŸ’Ύ Total Formats: {len(file_paths)}
⏱️  Generated: {doc_info['created_at']}

πŸ”— Share this ID: {doc_id}
   (Users can use this ID to access the document)
"""

        return result

    def create_preview_section(self, doc_id: str, max_chars: int = 1000) -> str:
        """
        Create a preview section of the document.

        Args:
            doc_id: Document ID
            max_chars: Maximum characters to show

        Returns:
            Preview text
        """
        doc_info = self.get_document_info(doc_id)
        if not doc_info:
            return "❌ Document not found"

        preview = doc_info["content_preview"]
        if len(preview) > max_chars:
            preview = preview[:max_chars] + f"\n... [{len(preview) - max_chars} more characters]"

        return f"""
πŸ“ DOCUMENT PREVIEW:
{'─' * 50}
{preview}
{'─' * 50}
"""

    def get_file_bytes(self, doc_id: str, format_type: str) -> Optional[bytes]:
        """
        Get file bytes for download.

        Args:
            doc_id: Document ID
            format_type: Format type (PDF, Word, etc.)

        Returns:
            File bytes or None
        """
        file_path = self.get_download_link(doc_id, format_type)
        if file_path and os.path.exists(file_path):
            try:
                with open(file_path, 'rb') as f:
                    return f.read()
            except Exception as e:
                logger.error(f"Error reading file {file_path}: {str(e)}")
                return None
        return None


class DocumentDisplayFormatter:
    """
    Format documents for display in Gradio interface.
    """

    @staticmethod
    def format_generation_result_with_download(
        result_text: str,
        doc_id: str,
        file_paths: Dict[str, str],
        preview_manager: DocumentPreviewManager
    ) -> str:
        """
        Format generation result with download links.

        Args:
            result_text: Original generation result text
            doc_id: Document ID
            file_paths: Generated file paths
            preview_manager: DocumentPreviewManager instance

        Returns:
            Formatted text with download info
        """
        download_interface = preview_manager.generate_download_interface(doc_id)

        return f"""{result_text}

{download_interface}

🎯 NEXT STEPS:
  1. Click on any format below to download
  2. Or use Document ID '{doc_id}' to access from another browser
  3. All formats contain the same content, choose your preferred format
"""

    @staticmethod
    def create_download_buttons_html(
        doc_id: str,
        formats_available: List[str]
    ) -> str:
        """
        Create HTML with download buttons.

        Args:
            doc_id: Document ID
            formats_available: List of available formats

        Returns:
            HTML for download buttons
        """
        html = f"""
        <div style="margin: 20px 0; padding: 15px; background: #e7f3ff; border-radius: 8px; border-left: 4px solid #007bff;">
            <h3 style="margin-top: 0; color: #004085;">πŸ“₯ Download Your Document</h3>
            <p style="color: #004085;">Document ID: <code style="background: #fff; padding: 2px 6px;">{doc_id}</code></p>
            <div style="display: flex; gap: 10px; flex-wrap: wrap;">
        """

        format_colors = {
            "PDF": "#dc3545",
            "WORD": "#0078d4",
            "MARKDOWN": "#6f42c1",
            "HTML": "#fd7e14",
            "LATEX": "#28a745"
        }

        for fmt in formats_available:
            fmt_upper = fmt.upper()
            color = format_colors.get(fmt_upper, "#6c757d")
            html += f"""
                <a href="#" onclick="downloadDocument('{doc_id}', '{fmt}')" 
                   style="padding: 10px 20px; background: {color}; color: white; 
                          text-decoration: none; border-radius: 4px; font-weight: bold;
                          cursor: pointer; display: inline-block;">
                    ⬇️ Download {fmt_upper}
                </a>
            """

        html += """
            </div>
        </div>
        """

        return html

    @staticmethod
    def format_document_list(preview_manager: DocumentPreviewManager) -> str:
        """
        Format list of all generated documents.

        Args:
            preview_manager: DocumentPreviewManager instance

        Returns:
            Formatted document list
        """
        all_docs = preview_manager.get_all_documents()

        if not all_docs:
            return "πŸ“­ No documents generated yet"

        result = """
╔══════════════════════════════════════════════════════════════╗
β•‘                  πŸ“„ GENERATED DOCUMENTS                      β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

"""

        for i, (doc_id, doc_info) in enumerate(all_docs.items(), 1):
            formats_str = ", ".join(doc_info["file_paths"].keys())
            result += f"""
{i}. {doc_info['title']}
   ID: {doc_id}
   Formats: {formats_str}
   Created: {doc_info['created_at']}
   Accessed: {doc_info['access_count']} times

"""

        return result

    @staticmethod
    def format_download_status(doc_id: str, format_type: str, success: bool, file_path: str = "") -> str:
        """Format download status message."""
        if success:
            size_mb = os.path.getsize(file_path) / (1024 * 1024)
            return f"""
βœ… DOWNLOAD SUCCESSFUL

πŸ“„ Format: {format_type}
πŸ“¦ File Size: {size_mb:.2f} MB
πŸ’Ύ Saved to: {file_path}

Your document is ready to use!
"""
        else:
            return f"""
❌ DOWNLOAD FAILED

Format: {format_type}
Document ID: {doc_id}

Please try again or contact support.
"""


class DocumentAccessor:
    """
    Access and retrieve generated documents.
    """

    def __init__(self, preview_manager: DocumentPreviewManager):
        """Initialize document accessor."""
        self.preview_manager = preview_manager

    def get_document_for_download(self, doc_id: str, format_type: str) -> Tuple[bool, bytes, str]:
        """
        Get document bytes for download.

        Args:
            doc_id: Document ID
            format_type: Format type

        Returns:
            Tuple of (success, file_bytes, filename)
        """
        doc_info = self.preview_manager.get_document_info(doc_id)
        if not doc_info:
            return False, b"", ""

        file_path = self.preview_manager.get_download_link(doc_id, format_type)
        if not file_path or not os.path.exists(file_path):
            return False, b"", ""

        try:
            with open(file_path, 'rb') as f:
                file_bytes = f.read()

            filename = f"{doc_info['title'].replace(' ', '_')}.{format_type.lower()}"
            return True, file_bytes, filename

        except Exception as e:
            logger.error(f"Error accessing document: {str(e)}")
            return False, b"", ""

    def get_preview(self, doc_id: str) -> str:
        """Get document preview."""
        return self.preview_manager.create_preview_section(doc_id)

    def list_available_formats(self, doc_id: str) -> List[str]:
        """List available formats for a document."""
        return self.preview_manager.get_formats_available(doc_id)

    def get_document_info_formatted(self, doc_id: str) -> str:
        """Get formatted document information."""
        doc_info = self.preview_manager.get_document_info(doc_id)
        if not doc_info:
            return "❌ Document not found"

        return f"""
πŸ“„ Document Information:
  Title: {doc_info['title']}
  ID: {doc_id}
  Created: {doc_info['created_at']}
  Formats: {', '.join(doc_info['file_paths'].keys())}
  Accessed: {doc_info['access_count']} times
"""