"""Dedupe — SHA256(url) gegen rolling state."""
from __future__ import annotations

import hashlib
import logging
from pathlib import Path

log = logging.getLogger(__name__)


def _hash_url(url: str) -> str:
    return hashlib.sha256(url.encode("utf-8")).hexdigest()


def _load_seen(state_file: Path) -> set[str]:
    if not state_file.exists():
        return set()
    return {line.strip() for line in state_file.read_text(encoding="utf-8").splitlines() if line.strip()}


def filter_unseen(items: list[dict], state_file: Path) -> list[dict]:
    """Behalte nur Items deren URL-Hash noch nicht in state_file ist."""
    seen = _load_seen(state_file)
    fresh: list[dict] = []
    for item in items:
        h = _hash_url(item["url"])
        if h not in seen:
            fresh.append(item)
    return fresh


def mark_seen(items: list[dict], state_file: Path) -> None:
    """Hänge URL-Hashes der Items an state_file an."""
    state_file.parent.mkdir(parents=True, exist_ok=True)
    with state_file.open("a", encoding="utf-8") as fh:
        for item in items:
            fh.write(_hash_url(item["url"]) + "\n")
