"""
Path utilities for TLS Tunnel Client
"""

import os
from typing import Optional


def resolve_path(path: Optional[str], base_dir: Optional[str] = None) -> Optional[str]:
    """
    Resolve a file path relative to a base directory.

    Args:
        path (str): Path to resolve (absolute or relative)
        base_dir (str, optional): Directory to resolve relative paths from.
            Defaults to None (current working directory)

    Returns:
        str: Absolute path
    """
    if path is None:
        return None
    if not os.path.isabs(path) and base_dir:
        return os.path.join(base_dir, path)
    return path


def ensure_dir(path: str):
    """Ensure a directory exists."""

    os.makedirs(path, exist_ok=True)


def is_file_exists(path, logger):
    """Check if file exists."""
    if os.path.exists(path):
        logger.debug(f"File exists: {path}")
        return True

    logger.error(f"File not found: {path}")
    return False
