from __future__ import annotations

from pathlib import Path

from .models import MountEntry, ValidationResult


def validate_paths(include_entries: list[MountEntry], review_entries: list[MountEntry]) -> ValidationResult:
    missing: list[MountEntry] = []
    present: list[MountEntry] = []

    for entry in [*include_entries, *review_entries]:
        entry.exists = entry.source.exists()
        if entry.exists:
            present.append(entry)
        else:
            missing.append(entry)

    return ValidationResult(missing=missing, present=present)


def mkdir_target_for_missing(entry: MountEntry) -> Path:
    """
    Heuristic:
    - If path looks like a file path (has suffix), create parent directory.
    - Otherwise create the directory path itself.
    """
    source = entry.source
    if source.suffix and not source.name.startswith("."):
        return source.parent
    return source


def apply_mkdir_for_missing(missing: list[MountEntry]) -> list[Path]:
    created: list[Path] = []

    for entry in missing:
        target = mkdir_target_for_missing(entry)
        if target.exists():
            continue
        target.mkdir(parents=True, exist_ok=True)
        created.append(target)

    return created
