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
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
"""
Repository File Type Chunker - Universal chunker for all file types.

This module provides file-type-aware chunking for repositories, handling
everything from Python code to configuration files, documentation, and
special files. It's the universal interface that delegates to specialized
chunkers based on file type.

ARCHITECTURE POSITION:
    - File Type Dispatcher: Routes files to appropriate chunkers
    - Universal Interface: Single entry point for all file types
    - Metadata Enricher: Adds repository context to all chunks

KEY FEATURES:
    1. File type detection and intelligent routing
    2. Hierarchical chunking for Python files
    3. Documentation chunking for markdown/RST
    4. Configuration file handling (JSON/YAML/TOML)
    5. Special file handling (README, requirements.txt, Dockerfile)
    6. Binary file detection and skipping

FILE TYPE SUPPORT:
    - .py: HierarchicalChunker (AST + Tree-sitter)
    - .md/.mdx/.rst: Documentation chunker
    - .json/.yaml/.toml: Configuration chunker
    - requirements.txt/Dockerfile: Special chunker
    - .txt/.cfg/.ini: Text chunker
    - README/LICENSE: Documentation chunker
    - Others: Text chunker with binary detection

DATA FLOW:
    File → Type detection → Route to specialized chunker → 
    Add repo metadata → Return chunks

USAGE:
    chunker = RepoChunker()
    chunks = chunker.chunk_file(Path("file.py"), repo_metadata)
"""

from pathlib import Path
from typing import List, Dict, Optional, cast
import json
import yaml
import re
import hashlib
from .hierarchical_chunker import HierarchicalChunker
from .chunk_schema import CodeChunk, ChunkAST, ChunkSpan, ChunkHierarchy, ChunkType, ASTSymbolType
from .doc_chunker import chunk_document as chunk_markdown_file


class RepoChunker:
    """
    Repository chunker that handles ALL file types with proper structure
    """
    
    def __init__(self, use_hierarchical: bool = True):
        if use_hierarchical:
            self.hierarchical_chunker = HierarchicalChunker()
        self.use_hierarchical = use_hierarchical
    
    def _generate_stable_id(self, content: str, prefix: str = "stable") -> str:
        """
        Generate deterministic chunk ID using SHA256.
        
        IMPORTANT: This ensures IDs are stable across runs, processes,
        and Python versions - crucial for RAG reproducibility.
        
        Args:
            content: The text content to hash
            prefix: ID prefix (config, doc, text, etc.)
            
        Returns:
            Deterministic ID like "config_8a3b2c1d"
        """
        # Use SHA256 for consistency with id_utils.py
        hash_digest = hashlib.sha256(content.encode("utf-8")).hexdigest()[:8]
        return f"{prefix}_{hash_digest}"
    
    def chunk_file(self, file_path: Path, repo_metadata: Optional[Dict] = None) -> List[CodeChunk]:
        """
        Chunk ANY file type with repository context
        
        Args:
            file_path: Path to the file
            repo_metadata: Optional dict with repo metadata
        """
        suffix = file_path.suffix.lower()
        
        # Python files - use your advanced hierarchical chunker
        if suffix == '.py':
            return self._chunk_python_file(file_path, repo_metadata)
        
        # Markdown/RST documentation
        elif suffix in ['.md', '.mdx', '.rst']:
            return self._chunk_markdown_file_wrapper(file_path, repo_metadata)
        
        # JSON config files
        elif suffix == '.json':
            return self._chunk_json_file(file_path, repo_metadata)
        
        # YAML/TOML config files
        elif suffix in ['.yaml', '.yml', '.toml']:
            return self._chunk_config_file(file_path, repo_metadata)
        
        # Requirements/Docker files
        elif file_path.name.lower() in ['requirements.txt', 'dockerfile', 'docker-compose.yml']:
            return self._chunk_special_file(file_path, repo_metadata)
        
        # Text files
        elif suffix in ['.txt', '.cfg', '.ini', '.conf']:
            return self._chunk_text_file(file_path, repo_metadata)
        
        # README/LICENSE files
        elif file_path.name.lower() in ['readme', 'readme.md', 'license', 'license.txt', 'license.md']:
            return self._chunk_readme_file(file_path, repo_metadata)
        
        # All other files
        else:
            return self._chunk_other_file(file_path, repo_metadata)
    
    def _chunk_python_file(self, file_path: Path, repo_metadata: Optional[Dict]) -> List[CodeChunk]:
        """Use our hierarchical chunker for Python files"""
        try:
            if self.use_hierarchical:
                chunks = self.hierarchical_chunker.chunk_file(file_path)
            else:
                # Fallback to basic text chunking instead of hybrid
                return self._chunk_text_file(file_path, repo_metadata)
            
            # Add repository metadata
            if repo_metadata:
                for chunk in chunks:
                    if "repo_info" not in chunk.metadata:
                        chunk.metadata["repo_info"] = {}
                    chunk.metadata["repo_info"].update(repo_metadata)
            
            return chunks
            
        except Exception as e:
            print(f"[ERROR] Error chunking Python file {file_path}: {e}")
            return self._chunk_text_file(file_path, repo_metadata)
    
    def _chunk_markdown_file_wrapper(self, file_path: Path, repo_metadata: Optional[Dict]) -> List[CodeChunk]:
        """Chunk markdown files using our doc_chunker"""
        try:
            content = file_path.read_text(encoding='utf-8', errors='ignore')
            
            # Use your existing doc_chunker
            doc_chunks = chunk_markdown_file(
                content, 
                source_name=file_path.name,
                source_url=f"file://{file_path}"
            )
            
            # Convert to CodeChunk schema
            code_chunks = []
            for doc_chunk in doc_chunks:
                code_chunk = CodeChunk(
                    chunk_id=doc_chunk["chunk_id"],  # Already uses SHA1 from doc_chunker.py
                    file_path=str(file_path),
                    language=doc_chunk.get("language", "markdown"),
                    chunk_type="documentation",
                    code=doc_chunk["content"],
                    ast=ChunkAST(
                        symbol_type="documentation",
                        name=file_path.name,
                        parent=None,
                        docstring=None
                    ),
                    span=ChunkSpan(
                        start_line=doc_chunk.get("metadata", {}).get("line_start", 1),
                        end_line=doc_chunk.get("metadata", {}).get("line_end", 1)
                    ),
                    metadata={
                        "doc_chunk_type": doc_chunk.get("chunk_type", "text"),
                        "repo_info": repo_metadata or {},
                        **doc_chunk.get("metadata", {})
                    },
                    hierarchy=ChunkHierarchy(
                        is_primary=True,
                        is_extracted=False,
                        depth=0
                    )
                )
                code_chunks.append(code_chunk)
            
            return code_chunks
            
        except Exception as e:
            print(f"[ERROR] Error chunking markdown file {file_path}: {e}")
            return self._chunk_text_file(file_path, repo_metadata)
    
    def _chunk_json_file(self, file_path: Path, repo_metadata: Optional[Dict]) -> List[CodeChunk]:
        """Chunk JSON config files"""
        try:
            content = file_path.read_text(encoding='utf-8', errors='ignore')
            data = json.loads(content)
            
            pretty_content = json.dumps(data, indent=2)
            
            # FIXED: Use deterministic SHA256 instead of hash()
            chunk = CodeChunk(
                chunk_id=self._generate_stable_id(pretty_content, "config"),
                file_path=str(file_path),
                language="json",
                chunk_type="configuration",
                code=pretty_content,
                ast=ChunkAST(
                    symbol_type="configuration",
                    name=file_path.name,
                    parent=None,
                    docstring=None
                ),
                span=ChunkSpan(
                    start_line=1,
                    end_line=len(pretty_content.split('\n'))
                ),
                metadata={
                    "file_type": "json_config",
                    "config_keys": list(data.keys()) if isinstance(data, dict) else [],
                    "repo_info": repo_metadata or {}
                },
                hierarchy=ChunkHierarchy(
                    is_primary=True,
                    is_extracted=False,
                    depth=0
                )
            )
            
            return [chunk]
            
        except Exception as e:
            print(f"[ERROR] Error chunking JSON file {file_path}: {e}")
            return self._chunk_text_file(file_path, repo_metadata)
    
    def _chunk_config_file(self, file_path: Path, repo_metadata: Optional[Dict]) -> List[CodeChunk]:
        """Chunk YAML/TOML config files"""
        try:
            content = file_path.read_text(encoding='utf-8', errors='ignore')
            suffix = file_path.suffix.lower()
            
            language = "yaml" if suffix in ['.yaml', '.yml'] else "toml"
            
            # FIXED: Use deterministic SHA256 instead of hash()
            chunk = CodeChunk(
                chunk_id=self._generate_stable_id(content, "config"),
                file_path=str(file_path),
                language=language,
                chunk_type="configuration",
                code=content,
                ast=ChunkAST(
                    symbol_type="configuration",
                    name=file_path.name,
                    parent=None,
                    docstring=None
                ),
                span=ChunkSpan(
                    start_line=1,
                    end_line=len(content.split('\n'))
                ),
                metadata={
                    "file_type": f"{language}_config",
                    "repo_info": repo_metadata or {}
                },
                hierarchy=ChunkHierarchy(
                    is_primary=True,
                    is_extracted=False,
                    depth=0
                )
            )
            
            return [chunk]
            
        except Exception as e:
            print(f"[ERROR] Error chunking config file {file_path}: {e}")
            return self._chunk_text_file(file_path, repo_metadata)
    
    def _chunk_special_file(self, file_path: Path, repo_metadata: Optional[Dict]) -> List[CodeChunk]:
        """Chunk special files (requirements.txt, Dockerfile, etc.)"""
        try:
            content = file_path.read_text(encoding='utf-8', errors='ignore')
            file_name = file_path.name.lower()
            
            if 'requirements' in file_name:
                language = "requirements"
                chunk_type = "configuration"
                prefix = "config"
            elif 'docker' in file_name:
                language = "dockerfile"
                chunk_type = "script"
                prefix = "script"
            else:
                language = "text"
                chunk_type = "text"
                prefix = "text"
            
            # FIXED: Use deterministic SHA256 instead of hash()
            chunk = CodeChunk(
                chunk_id=self._generate_stable_id(content, prefix),
                file_path=str(file_path),
                language=language,
                chunk_type=chunk_type,
                code=content,
                ast=ChunkAST(
                    symbol_type=chunk_type,
                    name=file_path.name,
                    parent=None,
                    docstring=None
                ),
                span=ChunkSpan(
                    start_line=1,
                    end_line=len(content.split('\n'))
                ),
                metadata={
                    "file_type": file_name,
                    "repo_info": repo_metadata or {},
                    "dependencies": self._extract_dependencies(content) if "requirements" in file_name else []
                },
                hierarchy=ChunkHierarchy(
                    is_primary=True,
                    is_extracted=False,
                    depth=0
                )
            )
            
            return [chunk]
            
        except Exception as e:
            print(f"[ERROR] Error chunking special file {file_path}: {e}")
            return self._chunk_text_file(file_path, repo_metadata)
    
    def _chunk_text_file(self, file_path: Path, repo_metadata: Optional[Dict]) -> List[CodeChunk]:
        """Chunk plain text files"""
        try:
            content = file_path.read_text(encoding='utf-8', errors='ignore')
            
            # Create a single chunk for small files, multiple for large ones
            if len(content.split('\n')) <= 200:
                chunks = [self._create_text_chunk(content, file_path, repo_metadata)]
            else:
                # Split large text files into reasonable chunks
                chunks = []
                lines = content.split('\n')
                chunk_size = 100
                
                for i in range(0, len(lines), chunk_size):
                    chunk_lines = lines[i:i + chunk_size]
                    chunk_content = '\n'.join(chunk_lines)
                    
                    chunk = self._create_text_chunk(
                        chunk_content, 
                        file_path, 
                        repo_metadata,
                        chunk_index=i // chunk_size
                    )
                    chunks.append(chunk)
            
            return chunks
            
        except Exception as e:
            print(f"[ERROR] Error reading text file {file_path}: {e}")
            return []
    
    def _chunk_readme_file(self, file_path: Path, repo_metadata: Optional[Dict]) -> List[CodeChunk]:
        """Special handling for README/LICENSE files"""
        try:
            content = file_path.read_text(encoding='utf-8', errors='ignore')
            file_name_lower = file_path.name.lower()
            
            # Determine appropriate prefix
            if 'readme' in file_name_lower:
                prefix = "doc"
            elif 'license' in file_name_lower:
                prefix = "license"
            else:
                prefix = "doc"
            
            # FIXED: Use deterministic SHA256 instead of hash()
            chunk = CodeChunk(
                chunk_id=self._generate_stable_id(content, prefix),
                file_path=str(file_path),
                language="markdown" if file_path.suffix in ['.md', '.mdx'] else "text",
                chunk_type="documentation",
                code=content,
                ast=ChunkAST(
                    symbol_type="documentation",
                    name=file_path.name,
                    parent=None,
                    docstring=None
                ),
                span=ChunkSpan(
                    start_line=1,
                    end_line=len(content.split('\n'))
                ),
                metadata={
                    "file_type": "readme_license",
                    "is_readme": "readme" in file_name_lower,
                    "is_license": "license" in file_name_lower,
                    "repo_info": repo_metadata or {}
                },
                hierarchy=ChunkHierarchy(
                    is_primary=True,
                    is_extracted=False,
                    depth=0
                )
            )
            
            return [chunk]
            
        except Exception as e:
            print(f"[ERROR] Error chunking README file {file_path}: {e}")
            return self._chunk_text_file(file_path, repo_metadata)
    
    def _chunk_other_file(self, file_path: Path, repo_metadata: Optional[Dict]) -> List[CodeChunk]:
        """Fallback for unknown file types (binary or unsupported)"""
        try:
            # Try to read as text first
            content = file_path.read_text(encoding='utf-8', errors='ignore')
            
            # If it looks like binary (mostly non-printable characters)
            if self._looks_like_binary(content):
                print(f"[SKIPPED] Skipping binary file: {file_path}")
                return []
            
            # If readable text, treat as text file
            return self._chunk_text_file(file_path, repo_metadata)
            
        except UnicodeDecodeError:
            print(f"[SKIPPED] Skipping binary file: {file_path}")
            return []
        except Exception as e:
            print(f"[ERROR] Error with file {file_path}: {e}")
            return []
    
    def _create_text_chunk(self, content: str, file_path: Path, 
                          repo_metadata: Optional[Dict], chunk_index: int = 0) -> CodeChunk:
        """Helper to create a text chunk"""
        lines = content.split('\n')
        
        # ENHANCED: Use deterministic ID that includes chunk_index for uniqueness
        id_payload = f"{content}_{chunk_index}"
        
        return CodeChunk(
            chunk_id=self._generate_stable_id(id_payload, "text"),
            file_path=str(file_path),
            language="text",
            chunk_type="text",
            code=content,
            ast=ChunkAST(
                symbol_type="text",
                name=file_path.name,
                parent=None,
                docstring=None
            ),
            span=ChunkSpan(
                start_line=1,
                end_line=len(lines)
            ),
            metadata={
                "file_type": "text",
                "chunk_index": chunk_index,
                "total_lines": len(lines),
                "repo_info": repo_metadata or {}
            },
            hierarchy=ChunkHierarchy(
                is_primary=True,
                is_extracted=False,
                depth=0
            )
        )
    
    def _extract_dependencies(self, requirements_content: str) -> List[str]:
        """Extract package names from requirements.txt"""
        dependencies = []
        for line in requirements_content.split('\n'):
            line = line.strip()
            if line and not line.startswith('#'):
                # Extract package name (before version specifiers)
                package = line.split('==')[0].split('>=')[0].split('<=')[0].strip()
                if package:
                    dependencies.append(package)
        return dependencies
    
    def _looks_like_binary(self, content: str, threshold: float = 0.3) -> bool:
        """Check if content looks like binary data"""
        if not content:
            return False
        
        # Count printable vs non-printable characters
        printable = sum(1 for c in content if 32 <= ord(c) <= 126 or c in '\n\r\t')
        total = len(content)
        
        if total == 0:
            return False
        
        ratio = printable / total
        return ratio < threshold