"""Formatting utilities for media files"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/media/utils.ipynb.

# %% auto 0
__all__ = ['format_file_size', 'format_timestamp', 'matches_patterns']

# %% ../../nbs/media/utils.ipynb 3
from datetime import datetime
from pathlib import Path
from typing import List

# %% ../../nbs/media/utils.ipynb 5
def format_file_size(
    size_bytes: int  # Size in bytes
) -> str:  # Human-readable size string (e.g., "15.2 MB")
    """Format file size in human-readable format."""
    for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
        if size_bytes < 1024.0:
            if unit == 'B':
                return f"{size_bytes} {unit}"
            return f"{size_bytes:.1f} {unit}"
        size_bytes /= 1024.0
    return f"{size_bytes:.1f} PB"

# %% ../../nbs/media/utils.ipynb 7
def format_timestamp(
    timestamp: float  # Unix timestamp
) -> str:  # Human-readable date string
    """Format timestamp to human-readable date with relative time for recent files."""
    dt = datetime.fromtimestamp(timestamp)
    now = datetime.now()
    diff = now - dt

    if diff.days == 0:
        if diff.seconds < 3600:
            minutes = diff.seconds // 60
            if minutes < 1:
                return "just now"
            return f"{minutes} min ago"
        hours = diff.seconds // 3600
        return f"{hours} hour{'s' if hours > 1 else ''} ago"
    elif diff.days == 1:
        return "yesterday"
    elif diff.days < 7:
        return f"{diff.days} days ago"
    else:
        return dt.strftime("%Y-%m-%d")

# %% ../../nbs/media/utils.ipynb 9
def matches_patterns(
    path: str,  # File path to check
    patterns: List[str]  # List of glob patterns to match against
) -> bool:  # True if path matches any pattern
    """Check if path matches any of the exclude patterns."""
    path_obj = Path(path)
    for pattern in patterns:
        if path_obj.match(pattern):
            return True
    return False
