"""Validation logic for src tree structure."""

from pathlib import Path

from kdaquila_structure_lint.config import Config
from kdaquila_structure_lint.config._constants.defaults import DEFAULT_SUPPORTED_EXTENSIONS
from kdaquila_structure_lint.validation._functions.matches_any_pattern import matches_any_pattern
from kdaquila_structure_lint.validation._functions.validate_custom_folder import (
    validate_custom_folder,
)


def validate_src_tree(root: Path, config: Config) -> list[str]:
    """Validate src tree structure."""
    errors: list[str] = []
    children = {
        c.name
        for c in root.iterdir()
        if c.is_dir() and not matches_any_pattern(c.name, config.structure.ignored_folders)
    }

    # Validate all subdirectories in src/ as base folders
    # No exact match required - accept any folders

    source_files = [
        c.name for c in root.iterdir()
        if c.is_file() and c.suffix in DEFAULT_SUPPORTED_EXTENSIONS
    ]
    disallowed = [f for f in source_files if f not in config.structure.files_allowed_anywhere]
    if disallowed:
        errors.append(f"{root}: Files not allowed in root: {disallowed}")

    # Validate all actual subdirectories found in src/
    for child in children:
        base_path = root / child
        errors.extend(validate_custom_folder(base_path, config, depth=0))

    return errors
