import os
from pathlib import Path
from typing import Iterable

DEFAULT_SCAN_EXCLUDES = {
    ".git",
    ".venv",
    "venv",
    "node_modules",
    "__pycache__",
    ".pytest_cache",
}

COMPOSE_FILENAMES = (
    "docker-compose.yml",
    "docker-compose.yaml",
    "compose.yml",
    "compose.yaml",
)


def discover_compose_files(
    root: Path | str,
    excludes: Iterable[str] | None = None,
) -> list[Path]:

    root = Path(root).resolve()

    exclude_set = set(DEFAULT_SCAN_EXCLUDES)
    if excludes:
        exclude_set.update(x.strip() for x in excludes if x)

    found = set()

    for current_root, dirnames, filenames in os.walk(root, topdown=True):
        # 🚫 skip unwanted dirs
        dirnames[:] = sorted(
            d for d in dirnames if d not in exclude_set
        )

        current_path = Path(current_root)

        for filename in filenames:
            if filename in COMPOSE_FILENAMES:
                found.add((current_path / filename).resolve())

    return sorted(found)
