import re
import time
from pathlib import Path


def slugify(text: str) -> str:
    text = text.lower().strip()
    text = re.sub(r'[^\w\s-]', '', text)
    text = re.sub(r'[\s_]+', '-', text)
    text = re.sub(r'-+', '-', text)
    return text.strip('-')


def load_proxies(filepath: str) -> list:
    path = Path(filepath)
    if not path.exists():
        return []
    proxies = []
    for line in path.read_text().splitlines():
        line = line.strip()
        if not line:
            continue
        if line.startswith("http://") or line.startswith("https://"):
            proxies.append(line)
        elif "@" in line:
            proxies.append(f"http://{line}")
        else:
            parts = line.split(":")
            if len(parts) == 4:
                ip, port, user, pwd = parts
                proxies.append(f"http://{user}:{pwd}@{ip}:{port}")
            else:
                proxies.append(f"http://{line}")
    return proxies


def ensure_dir(folder: str) -> Path:
    p = Path(folder)
    p.mkdir(parents=True, exist_ok=True)
    return p


def current_epoch_ms() -> int:
    return int(time.time() * 1000)
