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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
"""
Export API for converting HTML/Markdown to PDF and DOCX

This module provides endpoints for exporting content from the mobile/web app
to various formats (PDF, DOCX, HTML, Markdown).
"""

from pathlib import Path
from io import BytesIO
from typing import Optional

from fastapi import APIRouter, HTTPException, Depends, Request
from fastapi.responses import Response
from pydantic import BaseModel, Field
from urllib.parse import quote
from core.utils.auth_utils import verify_and_get_user_id_from_jwt

try:
    from weasyprint import HTML, CSS
except (ImportError, OSError) as e:
    weasyprint_available = False
    print(f"[WARNING] WeasyPrint not available: {e}")
    print("[INFO] To fix on macOS, run: export DYLD_FALLBACK_LIBRARY_PATH=$(brew --prefix)/lib:$DYLD_FALLBACK_LIBRARY_PATH")
else:
    weasyprint_available = True

try:
    from docx import Document
    from docx.shared import Inches, Pt, RGBColor
    from docx.enum.text import WD_ALIGN_PARAGRAPH
except ImportError:
    docx_available = False
else:
    docx_available = True

try:
    from bs4 import BeautifulSoup
except ImportError:
    beautifulsoup_available = False
else:
    beautifulsoup_available = True


router = APIRouter(prefix="/export", tags=["export"])


class ExportRequest(BaseModel):
    """Request model for export endpoints"""
    content: str = Field(..., description="HTML content to export")
    fileName: str = Field(..., description="Base filename (without extension)")


class ExportResponse(BaseModel):
    """Response model for export endpoints"""
    success: bool
    message: str
    filename: Optional[str] = None


# Professional PDF styles matching the Next.js implementation
PDF_STYLES = """
  * { 
    margin: 0; 
    padding: 0; 
    box-sizing: border-box; 
  }
  
  body { 
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 11pt;
    line-height: 1.6;
    color: #1a1a1a;
    padding: 0;
    margin: 0;
    background: #fff;
  }

  h1, h2, h3, h4, h5, h6 {
    font-weight: 600;
    line-height: 1.3;
    color: #111;
    letter-spacing: -0.02em;
    page-break-after: avoid;
  }
  
  h1 { 
    font-size: 18pt; 
    margin: 24pt 0 12pt 0;
    padding-bottom: 6pt;
    border-bottom: 1pt solid #e0e0e0;
  }
  h1:first-child { margin-top: 0; }
  
  h2 { font-size: 15pt; margin: 20pt 0 10pt 0; }
  h2:first-child { margin-top: 0; }
  
  h3 { font-size: 13pt; margin: 16pt 0 8pt 0; }
  h3:first-child { margin-top: 0; }
  
  h4 { font-size: 11pt; margin: 14pt 0 6pt 0; }
  h4:first-child { margin-top: 0; }
  
  h5 { font-size: 11pt; margin: 12pt 0 4pt 0; }
  h5:first-child { margin-top: 0; }
  
  h6 { 
    font-size: 10pt; 
    margin: 12pt 0 4pt 0;
    color: #555;
    text-transform: uppercase;
    letter-spacing: 0.5pt;
  }
  h6:first-child { margin-top: 0; }

  p { margin: 8pt 0; line-height: 1.65; }
  p:first-child { margin-top: 0; }
  p:last-child { margin-bottom: 0; }
  
  strong, b { font-weight: 600; }
  em, i { font-style: italic; }
  u { text-decoration: underline; }
  s, strike, del { text-decoration: line-through; color: #666; }

  a { color: #0066cc; text-decoration: underline; }

  ul, ol { margin: 10pt 0; padding-left: 28pt; }
  ul:first-child, ol:first-child { margin-top: 0; }
  ul:last-child, ol:last-child { margin-bottom: 0; }
  
  li { margin: 4pt 0; line-height: 1.5; padding-left: 4pt; }
  li > ul, li > ol { margin: 4pt 0; }

  blockquote { 
    margin: 12pt 0;
    padding: 8pt 12pt;
    border-left: 3pt solid #e0e0e0;
    background: #fafafa;
    color: #555;
    font-style: italic;
    border-radius: 0 4pt 4pt 0;
  }

  code {
    font-family: 'Courier New', Consolas, 'Lucida Console', monospace;
    font-size: 10pt;
    background: #f4f4f5;
    padding: 2pt 4pt;
    border-radius: 2pt;
    color: #1a1a1a;
  }
  
  pre {
    margin: 12pt 0;
    padding: 12pt;
    background: #f4f4f5;
    border: 1pt solid #e4e4e7;
    border-radius: 4pt;
    overflow-x: auto;
    font-size: 9.5pt;
    line-height: 1.5;
    page-break-inside: avoid;
  }
  pre code {
    background: none;
    padding: 0;
    border-radius: 0;
  }

  table { 
    width: 100%;
    margin: 14pt 0;
    border-collapse: collapse; 
    font-size: 10.5pt;
    page-break-inside: avoid;
  }
  
  th { 
    padding: 8pt 10pt;
    border: 1pt solid #d0d0d0;
    text-align: left;
    font-weight: 600;
    background: #f8f9fa;
  }
  
  td { 
    padding: 8pt 10pt;
    border: 1pt solid #d0d0d0;
    text-align: left;
    vertical-align: top;
  }

  hr { border: none; border-top: 1pt solid #e0e0e0; margin: 20pt 0; }

  img { max-width: 100%; height: auto; display: block; margin: 12pt 0; page-break-inside: avoid; }

  @media print {
    body { font-size: 11pt; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    pre { white-space: pre-wrap; word-wrap: break-word; page-break-inside: avoid; }
    h1, h2, h3, h4, h5, h6 { page-break-after: avoid; }
    table { page-break-inside: avoid; }
    img { page-break-inside: avoid; }
    tr { page-break-inside: avoid; }
  }

  @page { size: A4; margin: 20mm; }
"""


def sanitize_filename(name: str) -> str:
    """Sanitize filename for safe file system use"""
    import re
    return re.sub(r'[^\w\s-]', '', name).strip()[:100] or 'document'


def preprocess_html(html: str) -> str:
    """Preprocess HTML for better conversion"""
    import re

    if not html or not isinstance(html, str):
        return '<p></p>'

    # If content is a full HTML document, extract just the body content
    # This prevents nested HTML documents when we wrap it later
    if '<!DOCTYPE' in html.upper() or '<html' in html.lower():
        # Try to extract body content
        body_match = re.search(r'<body[^>]*>(.*?)</body>', html, re.IGNORECASE | re.DOTALL)
        if body_match:
            html = body_match.group(1)

    # Remove script/style tags
    html = re.sub(r'<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>', '', html, flags=re.IGNORECASE)
    html = re.sub(r'<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>', '', html, flags=re.IGNORECASE)

    # Clean up empty paragraphs
    html = re.sub(r'<p>\s*<\/p>', '', html)

    return html.strip()


@router.post("/pdf")
async def export_to_pdf(
    request: ExportRequest,
    user_id: str = Depends(verify_and_get_user_id_from_jwt)
):
    """
    Export HTML content to PDF using WeasyPrint
    
    Requires authentication.
    Returns the PDF file directly for download.
    """
    if not weasyprint_available:
        raise HTTPException(
            status_code=503,
            detail="PDF export is not available. WeasyPrint is not installed."
        )
    
    try:
        content = request.content
        file_name = sanitize_filename(request.fileName)
        
        print(f"[PDF Export] User: {user_id}, File: {file_name}, Content length: {len(content)}")
        
        # Preprocess HTML
        preprocessed_html = preprocess_html(content)
        
        # Create full HTML document with styles
        full_html = f"""<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>{file_name}</title>
  <style>{PDF_STYLES}</style>
</head>
<body>
{preprocessed_html}
</body>
</html>"""
        
        # Generate PDF with WeasyPrint
        pdf_bytes = HTML(string=full_html).write_pdf()
        
        # Return PDF file
        encoded_filename = quote(f"{file_name}.pdf", safe="")
        return Response(
            content=pdf_bytes,
            media_type="application/pdf",
            headers={
                "Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"
            }
        )
                
    except Exception as e:
        print(f"❌ PDF export error: {e}")
        import traceback
        traceback.print_exc()
        raise HTTPException(
            status_code=500,
            detail=f"Failed to generate PDF: {str(e)}"
        )


@router.post("/docx")
async def export_to_docx(
    request: ExportRequest,
    user_id: str = Depends(verify_and_get_user_id_from_jwt)
):
    """
    Export HTML content to DOCX format
    
    Requires authentication.
    Returns the DOCX file directly for download.
    """
    if not docx_available or not beautifulsoup_available:
        raise HTTPException(
            status_code=503,
            detail="DOCX export is not available. Required libraries are not installed."
        )
    
    try:
        content = request.content
        file_name = sanitize_filename(request.fileName)

        print(f"[DOCX Export] User: {user_id}, File: {file_name}, Content length: {len(content)}")

        # Preprocess HTML to extract body content if it's a full document
        preprocessed_content = preprocess_html(content)

        # Create DOCX document with clean black/white styling
        doc = Document()

        # Set default font to clean black/white theme
        style = doc.styles['Normal']
        style.font.name = 'Calibri'
        style.font.size = Pt(11)
        style.font.color.rgb = RGBColor(26, 26, 26)  # #1a1a1a - dark gray/black

        # Parse HTML with BeautifulSoup
        soup = BeautifulSoup(preprocessed_content, 'html.parser')

        # Convert HTML elements to DOCX
        for element in soup.children:
            process_html_element(element, doc)
        
        # Save to BytesIO buffer
        buffer = BytesIO()
        doc.save(buffer)
        buffer.seek(0)
        
        # Return DOCX file
        encoded_filename = quote(f"{file_name}.docx", safe="")
        return Response(
            content=buffer.getvalue(),
            media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            headers={
                "Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"
            }
        )
        
    except Exception as e:
        print(f"❌ DOCX export error: {e}")
        raise HTTPException(
            status_code=500,
            detail=f"Failed to generate DOCX: {str(e)}"
        )


def process_html_element(element, doc, parent_paragraph=None):
    """Process HTML element and add to DOCX document"""
    if element.name is None:
        text = str(element).strip()
        if text and parent_paragraph:
            parent_paragraph.add_run(text)
        return

    if element.name == 'p':
        p = doc.add_paragraph()
        for child in element.children:
            process_inline_element(child, p)
            
    elif element.name in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
        level = int(element.name[1])
        heading = doc.add_heading(element.get_text(), level)
        # Ensure heading is black
        for run in heading.runs:
            run.font.color.rgb = RGBColor(17, 17, 17)  # #111 - dark black for headings
        
    elif element.name == 'ul':
        for li in element.find_all('li', recursive=False):
            p = doc.add_paragraph(style='List Bullet')
            for child in li.children:
                process_inline_element(child, p)
                
    elif element.name == 'ol':
        for li in element.find_all('li', recursive=False):
            p = doc.add_paragraph(style='List Number')
            for child in li.children:
                process_inline_element(child, p)
                
    elif element.name == 'blockquote':
        p = doc.add_paragraph()
        p.paragraph_format.left_indent = Inches(0.5)
        run = p.add_run(element.get_text())
        run.font.italic = True
        run.font.color.rgb = RGBColor(85, 85, 85)  # #555 - gray for blockquotes
        
    elif element.name == 'pre':
        p = doc.add_paragraph()
        run = p.add_run(element.get_text())
        run.font.name = 'Courier New'
        run.font.size = Pt(10)
        run.font.color.rgb = RGBColor(26, 26, 26)  # Black for code blocks
        
    elif element.name == 'table':
        process_table_element(element, doc)


def process_inline_element(element, paragraph):
    """Process inline HTML element and add to paragraph"""
    if element.name is None:
        text = str(element).strip()
        if text:
            paragraph.add_run(text)
        return
    
    if element.name == 'strong' or element.name == 'b':
        run = paragraph.add_run(element.get_text())
        run.bold = True
        
    elif element.name == 'em' or element.name == 'i':
        run = paragraph.add_run(element.get_text())
        run.italic = True
        
    elif element.name == 'u':
        run = paragraph.add_run(element.get_text())
        run.underline = True
        
    elif element.name == 'code':
        run = paragraph.add_run(element.get_text())
        run.font.name = 'Courier New'
        run.font.size = Pt(10)
        run.font.color.rgb = RGBColor(26, 26, 26)  # Black for inline code
        
    elif element.name == 'a':
        run = paragraph.add_run(element.get_text())
        run.font.color.rgb = RGBColor(26, 26, 26)  # Black instead of blue
        run.underline = True
        
    else:
        for child in element.children:
            process_inline_element(child, paragraph)


def process_table_element(table_element, doc):
    """Process HTML table and add to DOCX document"""
    rows = table_element.find_all('tr')
    if not rows:
        return
    
    max_cols = max(len(row.find_all(['td', 'th'])) for row in rows)
    if max_cols == 0:
        return
    
    table = doc.add_table(rows=len(rows), cols=max_cols)
    table.style = 'Table Grid'
    
    for i, row in enumerate(rows):
        cells = row.find_all(['td', 'th'])
        for j, cell in enumerate(cells):
            if j < max_cols:
                table_cell = table.rows[i].cells[j]
                table_cell.text = cell.get_text().strip()
                
                if cell.name == 'th':
                    for paragraph in table_cell.paragraphs:
                        for run in paragraph.runs:
                            run.bold = True
                            run.font.color.rgb = RGBColor(17, 17, 17)  # Black for table headers


@router.get("/health")
async def export_health_check():
    """Export service health check endpoint"""
    return {
        "status": "healthy",
        "service": "export-api",
        "pdf_available": weasyprint_available,
        "docx_available": docx_available and beautifulsoup_available,
    }