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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
"""Unified file manager for Research and Docs sources in the dashboard.

Provides a unified API over both ``research/`` and ``docs/`` directories
with proper MIME type detection, binary file serving, and directory trees.
"""

from __future__ import annotations

import json
import re
import subprocess
from http import HTTPStatus
from io import BytesIO
from pathlib import Path
from typing import Any, Iterator, cast
from urllib.parse import unquote

from .docs_source import DocumentationSource, create_docs_source
from .research_source import ResearchSource

# ---------------------------------------------------------------------------
# MIME type map
# ---------------------------------------------------------------------------

_MEDIA_TYPES: dict[str, str] = {
    ".html": "text/html; charset=utf-8",
    ".css": "text/css; charset=utf-8",
    ".js": "text/javascript; charset=utf-8",
    ".svg": "image/svg+xml",
    ".ico": "image/x-icon",
    ".png": "image/png",
    ".jpg": "image/jpeg",
    ".jpeg": "image/jpeg",
    ".gif": "image/gif",
    ".webp": "image/webp",
    ".bmp": "image/bmp",
    ".mp4": "video/mp4",
    ".webm": "video/webm",
    ".ogg": "video/ogg",
    ".mov": "video/quicktime",
    ".avi": "video/x-msvideo",
    ".mp3": "audio/mpeg",
    ".wav": "audio/wav",
    ".flac": "audio/flac",
    ".aac": "audio/aac",
    ".m4a": "audio/mp4",
    ".opus": "audio/opus",
    ".pdf": "application/pdf",
    ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
}

_BINARY_EXTENSIONS = frozenset(
    {
        ".png",
        ".jpg",
        ".jpeg",
        ".gif",
        ".webp",
        ".svg",
        ".mp4",
        ".webm",
        ".ogg",
        ".mov",
        ".avi",
        ".mp3",
        ".wav",
        ".flac",
        ".aac",
        ".m4a",
        ".opus",
        ".pdf",
        ".docx",
        ".xlsx",
        ".xls",
        ".ico",
        ".bmp",
    }
)


def _mime_type(ext: str) -> str:
    return _MEDIA_TYPES.get(ext.lower(), "application/octet-stream")


# ---------------------------------------------------------------------------
# Exceptions
# ---------------------------------------------------------------------------


class FileManagerError(Exception):
    """Base error for file manager operations."""


class SourceNotAvailableError(FileManagerError):
    """Raised when a requested source is not configured."""


class InvalidSourceError(FileManagerError):
    """Raised when an unknown source name is given."""


class PathTraversalError(FileManagerError):
    """Raised on path traversal attempts."""


# ---------------------------------------------------------------------------
# Markdown export helpers
# ---------------------------------------------------------------------------


def _escape_html(value: str) -> str:
    return (
        value.replace("&", "&")
        .replace("<", "&lt;")
        .replace(">", "&gt;")
        .replace('"', "&quot;")
    )


def _markdown_to_html(content: str, title: str = "Document") -> str:
    """Convert a simple Markdown document to a standalone HTML page."""
    lines = content.splitlines()
    html_parts: list[str] = []
    in_code = False
    code_lines: list[str] = []

    for raw_line in lines:
        line = raw_line.rstrip()
        if line.startswith("```"):
            if in_code:
                html_parts.append(
                    "<pre><code>"
                    + _escape_html("\n".join(code_lines))
                    + "</code></pre>"
                )
                code_lines = []
            in_code = not in_code
            continue
        if in_code:
            code_lines.append(raw_line)
            continue

        # Heading
        match = re.match(r"^(#{1,6})\s+(.*)$", line)
        if match:
            level = len(match.group(1))
            html_parts.append(f"<h{level}>{_escape_html(match.group(2))}</h{level}>")
            continue

        # Unordered list
        if re.match(r"^\s*[-*+]\s+", line):
            text = re.sub(r"^\s*[-*+]\s+", "", line)
            html_parts.append(f"<li>{_escape_html(text)}</li>")
            continue

        # Ordered list
        match = re.match(r"^(\s*)\d+\.\s+(.*)$", line)
        if match:
            html_parts.append(f"<li>{_escape_html(match.group(2))}</li>")
            continue

        # Empty line
        if not line.strip():
            html_parts.append("<br>")
            continue

        # Paragraph with inline formatting
        para = _inline_markdown(line)
        html_parts.append(f"<p>{para}</p>")

    # Wrap loose list items in <ul>
    result: list[str] = []
    list_buffer: list[str] = []
    for part in html_parts:
        if part.startswith("<li>"):
            list_buffer.append(part)
        else:
            if list_buffer:
                result.append("<ul>" + "".join(list_buffer) + "</ul>")
                list_buffer = []
            result.append(part)
    if list_buffer:
        result.append("<ul>" + "".join(list_buffer) + "</ul>")

    body = "\n".join(result)
    return f"""<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<title>{_escape_html(title)}</title>
<style>
body {{ font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; max-width: 720px; margin: 2rem auto; padding: 0 1rem; line-height: 1.6; color: #1e293b; }}
h1, h2, h3 {{ color: #0f172a; }}
code {{ background: #f1f5f9; padding: 0.15rem 0.3rem; border-radius: 4px; }}
pre {{ background: #f1f5f9; padding: 0.75rem; border-radius: 8px; overflow-x: auto; }}
ul {{ padding-left: 1.25rem; }}
li {{ margin: 0.25rem 0; }}
</style>
</head>
<body>
{body}
</body>
</html>"""


def _inline_markdown(line: str) -> str:
    """Apply inline Markdown formatting (bold, italic, code links)."""
    # Use placeholders to protect markup during escaping
    placeholders: dict[str, str] = {}
    counter = 0

    def _protect(pattern: str, text: str) -> str:
        nonlocal counter
        out = text
        for m in re.finditer(pattern, text):
            key = f"\x00PLACEHOLDER{counter}\x00"
            counter += 1
            placeholders[key] = m.group(0)
            out = out.replace(m.group(0), key, 1)
        return out

    protected = _protect(r"`[^`]+`", line)
    protected = _protect(r"\*\*[^*]+\*\*", protected)
    protected = _protect(r"__[^_]+__", protected)
    protected = _protect(r"\*[^*]+\*", protected)
    protected = _protect(r"_[^_]+_", protected)
    protected = _protect(r"\[[^\]]+\]\([^)]+\)", protected)

    escaped = _escape_html(protected)
    for key, original in placeholders.items():
        # Convert protected Markdown to HTML
        html = original
        html = re.sub(r"`([^`]+)`", r"<code>\1</code>", html)
        html = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", html)
        html = re.sub(r"__(.+?)__", r"<strong>\1</strong>", html)
        html = re.sub(r"\*(.+?)\*", r"<em>\1</em>", html)
        html = re.sub(r"_(.+?)_", r"<em>\1</em>", html)
        html = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", r'<a href="\2">\1</a>', html)
        escaped = escaped.replace(key, html)
    return escaped


def _iter_markdown_blocks(content: str) -> Iterator[dict[str, Any]]:
    """Yield Markdown blocks for DOCX export."""
    lines = content.splitlines()
    in_code = False
    code_lines: list[str] = []
    for raw_line in lines:
        line = raw_line.rstrip()
        if line.startswith("```"):
            if in_code:
                yield {"type": "code", "text": "\n".join(code_lines)}
                code_lines = []
            in_code = not in_code
            continue
        if in_code:
            code_lines.append(raw_line)
            continue

        match = re.match(r"^(#{1,6})\s+(.*)$", line)
        if match:
            yield {
                "type": "heading",
                "level": len(match.group(1)),
                "text": match.group(2),
            }
            continue

        if re.match(r"^\s*[-*+\d]\.\s+", line):
            text = re.sub(r"^\s*[-*+\d]\.\s+", "", line)
            yield {"type": "list", "text": text}
            continue

        if not line.strip():
            continue

        yield {"type": "paragraph", "text": line}


# ---------------------------------------------------------------------------
# File Manager
# ---------------------------------------------------------------------------


class FileManager:
    """Unified file manager for Research and Docs sources.

    Provides directory tree listing, file search, statistics, and
    content retrieval (text as JSON, binary as raw bytes).
    """

    def __init__(
        self,
        research_source: ResearchSource | None,
        docs_source: DocumentationSource | None,
        default_docs_root: Path,
    ) -> None:
        self._research = research_source
        self._docs = docs_source
        self._default_docs_root = default_docs_root

    # ------------------------------------------------------------------
    # Source resolution
    # ------------------------------------------------------------------

    def _resolve_source(self, source: str) -> tuple[Any, Path, str]:
        if source == "research":
            s = self._research
            if s is None or not s.is_available():
                raise SourceNotAvailableError("Research source is not configured.")
            return s, s.root(), "research"

        if source == "docs":
            docs = self._docs or create_docs_source(self._default_docs_root)
            return docs, docs.docs_root, "docs"

        raise InvalidSourceError(f"Unknown source: {source}")

    def _root(self, source: str) -> Path:
        """Return the filesystem root for a source."""
        return self._resolve_source(source)[1]

    # ------------------------------------------------------------------
    # Directory tree
    # ------------------------------------------------------------------

    def get_tree(self, source: str = "research") -> dict[str, Any]:
        """Return the full directory tree for a source."""
        try:
            _obj, root, source_name = self._resolve_source(source)
        except FileManagerError as exc:
            return {"available": False, "error": str(exc), "children": []}

        def _build(path: Path, rel_prefix: str = "") -> dict[str, Any]:
            children: list[dict[str, Any]] = []
            try:
                entries = sorted(path.iterdir())
            except PermissionError:
                return {
                    "name": path.name,
                    "path": rel_prefix or ".",
                    "type": "directory",
                    "children": [],
                }

            for child in entries:
                if child.name.startswith("."):
                    continue
                if child.name in {"__pycache__", ".git", ".venv", "node_modules"}:
                    continue

                if child.is_dir():
                    child_rel = str(child.relative_to(root)) if child != root else ""
                    children.append(_build(child, child_rel))
                elif child.is_file():
                    ext = child.suffix.lower()
                    children.append(
                        {
                            "name": child.name,
                            "path": str(child.relative_to(root)),
                            "type": "file",
                            "size_bytes": child.stat().st_size,
                            "ext": ext,
                            "is_binary": ext in _BINARY_EXTENSIONS,
                            "is_image": ext
                            in {
                                ".png",
                                ".jpg",
                                ".jpeg",
                                ".gif",
                                ".webp",
                                ".svg",
                                ".bmp",
                            },
                            "is_video": ext
                            in {".mp4", ".webm", ".ogg", ".mov", ".avi"},
                            "is_audio": ext
                            in {".mp3", ".wav", ".flac", ".aac", ".m4a", ".opus"},
                            "is_spreadsheet": ext in {".xlsx", ".xls", ".xlsm", ".ods"},
                            "is_document": ext in {".docx", ".doc"},
                        }
                    )

            result: dict[str, Any] = {
                "name": root.name if rel_prefix == "" else path.name,
                "path": rel_prefix or ".",
                "type": "directory",
                "children": children,
            }
            if source_name == "research":
                manifest_path = path / "manifest.json"
                if manifest_path.is_file():
                    try:
                        manifest_raw: object = json.loads(
                            manifest_path.read_text(encoding="utf-8")
                        )
                        if isinstance(manifest_raw, dict):
                            manifest = cast(dict[str, object], manifest_raw)
                            created_at = manifest.get("created_at") or manifest.get(
                                "timestamp"
                            )
                            if created_at is not None:
                                result["created_at"] = str(created_at)
                    except (OSError, json.JSONDecodeError):
                        pass
            return result

        tree = _build(root)
        tree["available"] = True
        return tree

    # ------------------------------------------------------------------
    # Search
    # ------------------------------------------------------------------

    def search(self, source: str, query: str) -> list[dict[str, Any]]:
        """Search files by name or path."""
        q = query.lower().strip()
        if not q or len(q) < 2:
            return []

        try:
            _obj, root, _ = self._resolve_source(source)
        except FileManagerError:
            return []

        results: list[dict[str, Any]] = []
        for f in sorted(root.rglob("*")):
            if not f.is_file() or f.name.startswith("."):
                continue
            rel = str(f.relative_to(root))
            if q in f.name.lower() or q in rel.lower():
                ext = f.suffix.lower()
                results.append(
                    {
                        "name": f.name,
                        "path": rel,
                        "size_bytes": f.stat().st_size,
                        "ext": ext,
                        "is_binary": ext in _BINARY_EXTENSIONS,
                    }
                )
        return results

    # ------------------------------------------------------------------
    # Statistics
    # ------------------------------------------------------------------

    def get_statistics(self) -> dict[str, Any]:
        """Return combined statistics for both sources."""
        stats: dict[str, Any] = {}
        for src_name in ("research", "docs"):
            try:
                _obj, root, _ = self._resolve_source(src_name)
                total = 0
                total_size = 0
                for f in root.rglob("*"):
                    if f.is_file() and not f.name.startswith("."):
                        total += 1
                        total_size += f.stat().st_size
                stats[src_name] = {
                    "available": True,
                    "total_files": total,
                    "total_size_bytes": total_size,
                }
            except FileManagerError:
                stats[src_name] = {
                    "available": False,
                    "total_files": 0,
                    "total_size_bytes": 0,
                }
        return {"sources": stats}

    # ------------------------------------------------------------------
    # Content retrieval
    # ------------------------------------------------------------------

    def get_content(
        self, source: str, file_path: str
    ) -> tuple[bytes | str, str | None, bool]:
        """Get file content.

        Returns:
            Tuple of (content, mime_type_or_None, is_binary).
            For text files: (str, None, False).
            For binary files: (bytes, mime_type, True).
        """
        if not file_path or ".." in file_path:
            raise PathTraversalError("Path traversal is not allowed.")

        try:
            _obj, root, _ = self._resolve_source(source)
        except FileManagerError:
            raise SourceNotAvailableError(f"Source '{source}' is not available.")

        candidate = (root / file_path).resolve()
        try:
            candidate.relative_to(root)
        except ValueError:
            raise PathTraversalError("Path traversal detected.")

        if not candidate.is_file():
            raise FileNotFoundError(f"File not found: {file_path}")

        ext = candidate.suffix.lower()

        if ext in _BINARY_EXTENSIONS:
            data = candidate.read_bytes()
            return data, _mime_type(ext), True

        content = candidate.read_text(encoding="utf-8", errors="replace")
        return content, None, False

    def _resolve_file(self, source: str, file_path: str) -> Path:
        """Resolve and validate a file path under a source root."""
        if not file_path or ".." in file_path:
            raise PathTraversalError("Path traversal is not allowed.")

        try:
            _obj, root, _ = self._resolve_source(source)
        except FileManagerError:
            raise SourceNotAvailableError(f"Source '{source}' is not available.")

        candidate = (root / file_path).resolve()
        try:
            candidate.relative_to(root)
        except ValueError:
            raise PathTraversalError("Path traversal detected.")
        return candidate

    def save_content(
        self, source: str, file_path: str, content: str, backup: bool = True
    ) -> dict[str, Any]:
        """Save text content to a file under the configured source root.

        Args:
            source: 'research' or 'docs'.
            file_path: Relative path inside the source root.
            content: New file content as UTF-8 text.
            backup: If True, the existing file is renamed to `<name>.bak`
                before writing.

        Returns:
            Dict with success status and path information.
        """
        candidate = self._resolve_file(source, file_path)

        # Only allow saving to existing files for safety. New files can be
        # created explicitly later if needed.
        if not candidate.is_file():
            raise FileNotFoundError(f"File not found: {file_path}")

        ext = candidate.suffix.lower()
        if ext in _BINARY_EXTENSIONS:
            raise FileManagerError("Binary files cannot be edited as text.")

        if backup and candidate.exists():
            backup_path = candidate.with_suffix(candidate.suffix + ".bak")
            # Remove stale backup to avoid collision
            if backup_path.exists():
                backup_path.unlink()
            candidate.rename(backup_path)

        candidate.write_text(content, encoding="utf-8")
        return {
            "success": True,
            "path": file_path,
            "size_bytes": candidate.stat().st_size,
            "backup_created": backup,
        }

    def _meta_path(self, source: str, file_path: str) -> Path:
        """Return the sidecar metadata path for a file."""
        candidate = self._resolve_file(source, file_path)
        return candidate.parent / (candidate.name + ".meta.yaml")

    def get_meta(self, source: str, file_path: str) -> dict[str, Any]:
        """Load sidecar metadata for a file.

        Args:
            source: 'research' or 'docs'.
            file_path: Relative path inside the source root.

        Returns:
            Dict with meta content and existence status.
        """
        candidate = self._resolve_file(source, file_path)
        if not candidate.is_file():
            raise FileNotFoundError(f"File not found: {file_path}")

        meta_path = self._meta_path(source, file_path)
        content = meta_path.read_text(encoding="utf-8") if meta_path.is_file() else ""
        return {
            "path": file_path,
            "meta_path": str(meta_path.relative_to(self._root(source))),
            "exists": meta_path.is_file(),
            "content": content,
        }

    def save_meta(
        self, source: str, file_path: str, content: str, backup: bool = True
    ) -> dict[str, Any]:
        """Save sidecar metadata for a file.

        Args:
            source: 'research' or 'docs'.
            file_path: Relative path inside the source root.
            content: YAML metadata text.
            backup: If True, existing meta file is backed up.

        Returns:
            Dict with success status and path information.
        """
        candidate = self._resolve_file(source, file_path)
        if not candidate.is_file():
            raise FileNotFoundError(f"File not found: {file_path}")

        meta_path = self._meta_path(source, file_path)
        if backup and meta_path.exists():
            backup_path = meta_path.with_suffix(".meta.yaml.bak")
            if backup_path.exists():
                backup_path.unlink()
            meta_path.rename(backup_path)

        meta_path.write_text(content, encoding="utf-8")
        return {
            "success": True,
            "path": file_path,
            "meta_path": str(meta_path.relative_to(self._root(source))),
            "size_bytes": meta_path.stat().st_size,
            "backup_created": backup,
        }

    def analyze_content(self, source: str, file_path: str) -> dict[str, Any]:
        """Return a lightweight local analysis of a text file.

        Args:
            source: 'research' or 'docs'.
            file_path: Relative path inside the source root.

        Returns:
            Dict with statistics, language guess, readability, keywords,
            sentiment and a generated summary.
        """
        candidate = self._resolve_file(source, file_path)
        if not candidate.is_file():
            raise FileNotFoundError(f"File not found: {file_path}")

        ext = candidate.suffix.lower()
        if ext in _BINARY_EXTENSIONS:
            raise FileManagerError("Binary files cannot be analyzed as text.")

        content = candidate.read_text(encoding="utf-8")
        if not content.strip():
            return {
                "path": file_path,
                "language": None,
                "stats": {"chars": 0, "words": 0, "lines": 0, "sentences": 0},
                "readability": None,
                "keywords": [],
                "sentiment": {"score": 0, "label": "neutral"},
                "summary": "",
            }

        import re

        # Basic tokenization
        text_lower = content.lower()
        words = re.findall(r"\b[a-zA-ZäöüßÄÖÜ]{3,}\b", text_lower)
        sentences = re.split(r"(?<=[.!?])\s+", content.strip())
        sentences = [s for s in sentences if s.strip()]

        # Language guess
        de_markers = sum(
            1
            for w in [
                "der",
                "die",
                "das",
                "und",
                "ist",
                "von",
                "mit",
                "für",
                "den",
                "dem",
            ]
            if w in words
        )
        en_markers = sum(
            1
            for w in ["the", "and", "is", "of", "to", "a", "in", "for", "that", "with"]
            if w in words
        )
        language = "de" if de_markers > en_markers else "en"

        # Stopwords
        stopwords = {
            "en": {
                "the",
                "and",
                "is",
                "of",
                "to",
                "a",
                "in",
                "for",
                "that",
                "with",
                "as",
                "on",
                "by",
                "this",
                "from",
                "it",
                "be",
                "are",
                "was",
                "were",
                "been",
                "have",
                "has",
                "had",
                "do",
                "does",
                "did",
                "will",
                "would",
                "could",
                "should",
                "may",
                "might",
                "can",
                "shall",
            },
            "de": {
                "der",
                "die",
                "das",
                "und",
                "ist",
                "von",
                "mit",
                "für",
                "den",
                "dem",
                "ein",
                "eine",
                "einer",
                "eines",
                "einem",
                "einen",
                "zu",
                "auf",
                "an",
                "in",
                "bei",
                "nach",
                "aus",
                "durch",
                "wie",
                "so",
                "wenn",
                "dann",
                "als",
                "auch",
                "noch",
                "nur",
                "oder",
                "aber",
                "sondern",
                "weil",
                "dass",
            },
        }[language]

        # Keywords by frequency
        filtered = [w for w in words if w not in stopwords]
        counts: dict[str, int] = {}
        for w in filtered:
            counts[w] = counts.get(w, 0) + 1
        keywords = sorted(counts.items(), key=lambda x: x[1], reverse=True)[:10]

        # Readability (simplified)
        word_count = len(words)
        sentence_count = max(len(sentences), 1)
        avg_sentence_length = word_count / sentence_count
        if language == "en":
            # Flesch Reading Ease approximation
            syllables = sum(max(1, len(re.findall(r"[aeiouy]+", w))) for w in words)
            asl = avg_sentence_length
            asw = syllables / max(word_count, 1)
            flesch = 206.835 - (1.015 * asl) - (84.6 * asw)
            readability = {"score": round(flesch, 2), "label": "flesch_reading_ease"}
        else:
            readability = {
                "score": round(avg_sentence_length, 2),
                "label": "avg_words_per_sentence",
            }

        # Sentiment
        positive = {
            "good",
            "great",
            "excellent",
            "positive",
            "success",
            "improve",
            "benefit",
            "best",
            "happy",
            "love",
            "nice",
            "easy",
            "strong",
            "gut",
            "grossartig",
            "ausgezeichnet",
            "positiv",
            "erfolg",
            "verbessern",
            "vorteil",
            "beste",
            "glücklich",
            "liebe",
            "schön",
            "einfach",
            "stark",
        }
        negative = {
            "bad",
            "error",
            "fail",
            "problem",
            "bug",
            "issue",
            "wrong",
            "poor",
            "negative",
            "difficult",
            "schlecht",
            "fehler",
            "scheitern",
            "problem",
            "bug",
            "problem",
            "falsch",
            "arm",
            "negativ",
            "schwierig",
        }
        pos_count = sum(1 for w in words if w in positive)
        neg_count = sum(1 for w in words if w in negative)
        sentiment_score = pos_count - neg_count
        if sentiment_score > 0:
            sentiment_label = "positive"
        elif sentiment_score < 0:
            sentiment_label = "negative"
        else:
            sentiment_label = "neutral"

        # Summary: first few sentences, capped
        summary_sentences = sentences[:3]
        summary = " ".join(summary_sentences)

        return {
            "path": file_path,
            "language": language,
            "stats": {
                "chars": len(content),
                "words": word_count,
                "lines": content.count("\n") + 1,
                "sentences": len(sentences),
            },
            "readability": readability,
            "keywords": [{"word": w, "count": c} for w, c in keywords],
            "sentiment": {"score": sentiment_score, "label": sentiment_label},
            "summary": summary,
        }

    def export_content(
        self, source: str, file_path: str, fmt: str
    ) -> tuple[str, bytes, str]:
        """Export a text file to HTML, DOCX or Markdown.

        Args:
            source: 'research' or 'docs'.
            file_path: Relative path inside the source root.
            fmt: 'html', 'docx' or 'md'.

        Returns:
            Tuple of (filename, bytes, mime_type).
        """
        candidate = self._resolve_file(source, file_path)
        if not candidate.is_file():
            raise FileNotFoundError(f"File not found: {file_path}")

        ext = candidate.suffix.lower()
        if ext in _BINARY_EXTENSIONS:
            raise FileManagerError("Binary files cannot be exported as documents.")

        content = candidate.read_text(encoding="utf-8")
        base_name = candidate.stem
        fmt = fmt.lower()

        if fmt == "md":
            return (
                f"{base_name}.md",
                content.encode("utf-8"),
                "text/markdown; charset=utf-8",
            )

        if fmt == "html":
            html = _markdown_to_html(content, title=base_name)
            return (
                f"{base_name}.html",
                html.encode("utf-8"),
                "text/html; charset=utf-8",
            )

        if fmt == "docx":
            try:
                from docx import Document
            except ImportError as exc:  # pragma: no cover - optional dependency
                raise FileManagerError(
                    "python-docx is not installed. Install it to enable DOCX export."
                ) from exc

            doc = Document()
            doc.add_heading(base_name, level=0)
            for block in _iter_markdown_blocks(content):
                if block["type"] == "heading":
                    doc.add_heading(block["text"], level=block["level"])
                elif block["type"] == "paragraph":
                    doc.add_paragraph(block["text"])
                elif block["type"] == "code":
                    doc.add_paragraph(block["text"], style="Quote")
                elif block["type"] == "list":
                    doc.add_paragraph(block["text"], style="List Bullet")
            out = BytesIO()
            doc.save(out)
            return (
                f"{base_name}.docx",
                out.getvalue(),
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            )

        raise FileManagerError(f"Unsupported export format: {fmt}")

    def get_history(
        self, source: str, file_path: str, limit: int = 20
    ) -> list[dict[str, Any]]:
        """Return git history for a file under the configured source root.

        Args:
            source: 'research' or 'docs'.
            file_path: Relative path inside the source root.
            limit: Maximum number of commits to return.

        Returns:
            List of commit dicts with hash, date, author, message.
        """
        candidate = self._resolve_file(source, file_path)
        if not candidate.is_file():
            raise FileNotFoundError(f"File not found: {file_path}")

        root = candidate.parent
        while not (root / ".git").is_dir() and root != root.parent:
            root = root.parent

        if not (root / ".git").is_dir():
            return []

        try:
            rel = candidate.relative_to(root)
        except ValueError:
            return []

        try:
            result = subprocess.run(
                [
                    "git",
                    "log",
                    f"-n{limit}",
                    "--follow",
                    "--oneline",
                    "--format=%H|%ad|%an|%s",
                    "--date=iso-strict",
                    "--",
                    str(rel),
                ],
                cwd=str(root),
                capture_output=True,
                text=True,
                encoding="utf-8",
                errors="replace",
                check=False,
            )
        except FileNotFoundError:
            return []

        if result.returncode != 0:
            return []

        history: list[dict[str, str]] = []
        for line in result.stdout.strip().splitlines():
            parts = line.split("|", 3)
            if len(parts) < 4:
                continue
            history.append(
                {
                    "hash": parts[0],
                    "date": parts[1],
                    "author": parts[2],
                    "message": parts[3],
                }
            )
        return history


# ---------------------------------------------------------------------------
# HTTP handler integration
# ---------------------------------------------------------------------------

_DEFAULT_DOCS_ROOT = Path(__file__).resolve().parents[2] / "docs"


def register_file_manager_routes(
    handler: Any,
    path: str,
    query: dict[str, list[str]],
    research_source: ResearchSource | None,
    docs_source: DocumentationSource | None,
) -> bool:
    """Try to handle a file manager API route. Returns True if handled."""
    fm = FileManager(research_source, docs_source, _DEFAULT_DOCS_ROOT)

    if path == "/api/files/tree":
        source = query.get("source", ["research"])[0]
        handler._send_json(fm.get_tree(source))
        return True

    if path == "/api/files/search":
        source = query.get("source", ["research"])[0]
        q = query.get("q", [""])[0]
        handler._send_json({"results": fm.search(source, q)})
        return True

    if path == "/api/files/statistics":
        handler._send_json(fm.get_statistics())
        return True

    if path == "/api/files/open":
        source = query.get("source", ["research"])[0]
        try:
            # fmt: off
            _obj, root, _name = fm._resolve_source(source)  # pyright: ignore[reportPrivateUsage]
            # fmt: on
            import os
            import subprocess

            if os.name == "nt":
                subprocess.Popen(["explorer", str(root.resolve())])
            else:
                subprocess.Popen(["xdg-open", str(root.resolve())])
            handler._send_json({"ok": True, "path": str(root.resolve())})
        except Exception as exc:
            handler._send_json({"ok": False, "error": str(exc)})
        return True

    if path.startswith("/api/files/content/"):
        prefix = "/api/files/content/"
        file_path = unquote(path[len(prefix) :])
        source = query.get("source", ["research"])[0]

        try:
            content, mime, is_binary = fm.get_content(source, file_path)
        except FileNotFoundError:
            handler._send_json(
                {"error": f"File not found: {file_path}"},
                HTTPStatus.NOT_FOUND,
            )
            return True
        except (SourceNotAvailableError, PathTraversalError) as exc:
            status = (
                HTTPStatus.NOT_FOUND
                if isinstance(exc, SourceNotAvailableError)
                else HTTPStatus.FORBIDDEN
            )
            handler._send_json({"error": str(exc)}, status)
            return True
        except OSError as exc:
            handler._send_json(
                {"error": str(exc)},
                HTTPStatus.INTERNAL_SERVER_ERROR,
            )
            return True

        if is_binary and mime:
            data = cast(bytes, content)
            handler.send_response(HTTPStatus.OK)
            handler.send_header("Content-Type", mime)
            handler.send_header("Content-Length", str(len(data)))
            handler.send_header("Cache-Control", "no-cache")
            handler.send_header("Access-Control-Allow-Origin", "*")
            handler.end_headers()
            handler.wfile.write(data)
        else:
            text_content = cast(str, content)
            handler._send_json(
                {
                    "path": file_path,
                    "name": Path(file_path).name,
                    "content": text_content,
                    "size_bytes": len(text_content.encode("utf-8")),
                    "ext": Path(file_path).suffix.lower(),
                    "is_binary": False,
                }
            )
        return True

    if path.startswith("/api/files/save/") and handler.command == "PUT":
        prefix = "/api/files/save/"
        file_path = unquote(path[len(prefix) :])
        source = query.get("source", ["research"])[0]
        try:
            body = handler._read_json_body()
        except Exception:
            handler._send_json(
                {"error": "Invalid JSON body"},
                HTTPStatus.BAD_REQUEST,
            )
            return True

        content = body.get("content")
        if content is None or not isinstance(content, str):
            handler._send_json(
                {"error": "Missing or invalid 'content' field"},
                HTTPStatus.BAD_REQUEST,
            )
            return True

        try:
            result = fm.save_content(
                source, file_path, content, backup=body.get("backup", True)
            )
            handler._send_json(result)
        except FileNotFoundError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.NOT_FOUND)
        except PathTraversalError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.FORBIDDEN)
        except FileManagerError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
        except OSError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.INTERNAL_SERVER_ERROR)
        return True

    if path.startswith("/api/files/history/"):
        prefix = "/api/files/history/"
        file_path = unquote(path[len(prefix) :])
        source = query.get("source", ["research"])[0]
        limit = int(query.get("limit", ["20"])[0])

        try:
            history = fm.get_history(source, file_path, limit=limit)
            handler._send_json({"path": file_path, "history": history})
        except FileNotFoundError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.NOT_FOUND)
        except PathTraversalError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.FORBIDDEN)
        return True

    if path.startswith("/api/files/meta/") and handler.command == "GET":
        prefix = "/api/files/meta/"
        file_path = unquote(path[len(prefix) :])
        source = query.get("source", ["research"])[0]

        try:
            result = fm.get_meta(source, file_path)
            handler._send_json(result)
        except FileNotFoundError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.NOT_FOUND)
        except PathTraversalError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.FORBIDDEN)
        return True

    if path.startswith("/api/files/meta/") and handler.command == "PUT":
        prefix = "/api/files/meta/"
        file_path = unquote(path[len(prefix) :])
        source = query.get("source", ["research"])[0]
        try:
            body = handler._read_json_body()
        except Exception:
            handler._send_json(
                {"error": "Invalid JSON body"},
                HTTPStatus.BAD_REQUEST,
            )
            return True

        content = body.get("content")
        if content is None or not isinstance(content, str):
            handler._send_json(
                {"error": "Missing or invalid 'content' field"},
                HTTPStatus.BAD_REQUEST,
            )
            return True

        try:
            result = fm.save_meta(
                source, file_path, content, backup=body.get("backup", True)
            )
            handler._send_json(result)
        except FileNotFoundError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.NOT_FOUND)
        except PathTraversalError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.FORBIDDEN)
        except FileManagerError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
        except OSError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.INTERNAL_SERVER_ERROR)
        return True

    if path.startswith("/api/files/analyze/"):
        prefix = "/api/files/analyze/"
        file_path = unquote(path[len(prefix) :])
        source = query.get("source", ["research"])[0]

        try:
            result = fm.analyze_content(source, file_path)
            handler._send_json(result)
        except FileNotFoundError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.NOT_FOUND)
        except PathTraversalError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.FORBIDDEN)
        except FileManagerError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
        return True

    if path.startswith("/api/files/export/"):
        prefix = "/api/files/export/"
        file_path = unquote(path[len(prefix) :])
        source = query.get("source", ["research"])[0]
        fmt = query.get("format", ["html"])[0]

        try:
            filename, data, mime = fm.export_content(source, file_path, fmt)
            handler.send_response(HTTPStatus.OK)
            handler.send_header("Content-Type", mime)
            handler.send_header(
                "Content-Disposition", f'attachment; filename="{filename}"'
            )
            handler.send_header("Content-Length", str(len(data)))
            handler.send_header("Cache-Control", "no-cache")
            handler.send_header("Access-Control-Allow-Origin", "*")
            handler.end_headers()
            handler.wfile.write(data)
        except FileNotFoundError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.NOT_FOUND)
        except PathTraversalError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.FORBIDDEN)
        except FileManagerError as exc:
            handler._send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
        return True

    return False