import os
from pathlib import Path
from typing import List, Tuple

class ProjectScanner:
    def __init__(self, root_path: str):
        self.root_path = Path(root_path)
        # Folders and extensions to ignore
        self.ignore_dirs = {'.git', 'venv', 'env', 'node_modules', '__pycache__', '.idea', '.vscode'}
        self.ignore_extensions = {'.pyc', '.pyo', '.so', '.dll', '.exe', '.png', '.jpg', '.jpeg', '.gif', '.ico'}

    def scan(self) -> List[Tuple[str, str]]:
        """
        Scans the project and returns a list of tuples: (relative_file_path, file_content)
        """
        files_data = []
        print(f"[bold blue]Scanning project at {self.root_path.resolve()}...[/bold blue]")
        
        for path in self.root_path.rglob('*'):
            if path.is_file():
                # Check ignored directories
                if any(ignore in path.parts for ignore in self.ignore_dirs):
                    continue
                
                # Check ignored extensions
                if path.suffix in self.ignore_extensions:
                    continue

                try:
                    # Read file content
                    with open(path, 'r', encoding='utf-8', errors='ignore') as f:
                        content = f.read()
                    
                    # Skip empty files or massive binary-like files
                    if not content.strip() or len(content) > 50000: 
                        continue

                    rel_path = path.relative_to(self.root_path)
                    files_data.append((str(rel_path), content))
                except Exception as e:
                    print(f"[yellow]Warning: Could not read {path}: {e}[/yellow]")

        print(f"[green]Found {len(files_data)} files to analyze.[/green]")
        return files_data