#!/usr/bin/env python3
"""
XDG Base Directory helpers.

Follows the XDG Base Directory Specification:
  ~/.config/    — configuration files
  ~/.local/share/ — persistent user data
  ~/.cache/     — temporary cache data
"""

import os

_DATA_DIR: str | None = None
_CONFIG_DIR: str | None = None
_CACHE_DIR: str | None = None


def xdg_config_path(subdir: str = "") -> str:
    global _CONFIG_DIR
    if _CONFIG_DIR is None:
        base = os.environ.get(
            "XDG_CONFIG_HOME",
            os.path.join(os.path.expanduser("~"), ".config"),
        )
        _CONFIG_DIR = os.path.join(base, "neo-pod-desktop")
    path = os.path.join(_CONFIG_DIR, subdir) if subdir else _CONFIG_DIR
    os.makedirs(path, exist_ok=True)
    return path


def xdg_data_path(subdir: str = "") -> str:
    global _DATA_DIR
    if _DATA_DIR is None:
        base = os.environ.get(
            "XDG_DATA_HOME",
            os.path.join(os.path.expanduser("~"), ".local", "share"),
        )
        _DATA_DIR = os.path.join(base, "neo-pod-desktop")
    path = os.path.join(_DATA_DIR, subdir) if subdir else _DATA_DIR
    os.makedirs(path, exist_ok=True)
    return path


def xdg_cache_path(subdir: str = "") -> str:
    global _CACHE_DIR
    if _CACHE_DIR is None:
        base = os.environ.get(
            "XDG_CACHE_HOME",
            os.path.join(os.path.expanduser("~"), ".cache"),
        )
        _CACHE_DIR = os.path.join(base, "neo-pod-desktop")
    path = os.path.join(_CACHE_DIR, subdir) if subdir else _CACHE_DIR
    os.makedirs(path, exist_ok=True)
    return path
