"""Miscellaneous functions."""

from __future__ import annotations

import datetime
from typing import Any


def to_list(value: Any | list[Any] | None) -> list[Any]:
    """
    Convert value into a list.

    - if value is ``None`` then return an empty list
    - if value is already a list then return it unchanged
    - if value is not a list then return ``[value]``

    :param value: Value that should be converted to a list.
    :type value: Any | list[Any] | None
    :return: List with the value as an element.
    :rtype: list[Any]
    """
    if value is None:
        return []

    if isinstance(value, list):
        return value

    return [value]


def to_list_of_strings(value: Any | list[Any] | None) -> list[str]:
    """
    Convert given list or single value to list of strings.

    The ``value`` is first converted to a list and then ``str(item)`` is run on
    each of its item.

    :param value: Value to convert.
    :type value: Any | list[Any] | None
    :return: List of strings.
    :rtype: list[str]
    """
    return [str(x) for x in to_list(value)]


def days_since_epoch():
    """
    Gets the current date and returns the number of days since 1970-01-01 UTC, this time is also referred to as the
    Unix epoch.
    """
    epoch = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
    now_utc = datetime.datetime.now(datetime.timezone.utc)
    delta = now_utc - epoch
    return delta.days


def shadow_password_pattern() -> str:
    """
    Returns the shadow password regex pattern.

    This pattern matches encrypted password hashes stored in /etc/shadow files,
    including common formats like MD5, SHA-256, SHA-512, and yescrypt. The pattern
    expects the standard Unix crypt format: $algorithm$salt$hash with optional parameters.
    """
    return r"^\$(?:\w)\$[^$]*\$[\w./]+(?:\$[\w./]+)?$"
