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

class FileScanner:
    """Scans the filesystem for potential data assets (CSV, JSON, SQL, etc.)."""
    
    DEFAULT_EXTENSIONS = {
        # Data Files
        '.csv', '.json', '.sql', '.db', '.sqlite', '.parquet', '.xlsx',
        # Sensitive Configs & Keys
        '.pem', '.key', '.env', '.p12', '.kdbx', '.log' 
    }

    SENSITIVE_KEYWORDS = {'backup', 'dump', 'secret', 'password', 'token', 'credential'}

    def __init__(self, search_path: str, extensions: Set[str] = None):
        self.search_path = Path(search_path).resolve()
        self.extensions = extensions or self.DEFAULT_EXTENSIONS
        self.detected_assets: Set[str] = set()

    def scan(self) -> Set[str]:
        if not self.search_path.exists():
            raise FileNotFoundError(f"Search path does not exist: {self.search_path}")

        for root, dirs, files in os.walk(self.search_path):
            # Skip hidden directories and virtual envs
            dirs[:] = [d for d in dirs if not d.startswith('.') and d not in {'venv', 'env', '__pycache__'}]
            
            for file in files:
                file_path = Path(root) / file
                file_lower = file.lower()
                suffix = file_path.suffix.lower()

                # Rule 1: Extension Match
                is_extension_match = suffix in self.extensions

                # Rule 2: Keyword Match (High Risk)
                is_keyword_match = any(kw in file_lower for kw in self.SENSITIVE_KEYWORDS)

                if is_extension_match or is_keyword_match:
                    self.detected_assets.add(str(file_path.resolve()))
                    
        return self.detected_assets
