from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from factory_decision_notification_store import FactoryDecisionNotificationStore


def test_record_notified_true_only_first_time(tmp_path: Path) -> None:
    store = FactoryDecisionNotificationStore(tmp_path / "seen.json")

    assert store.record_notified("dec-1") is True
    assert store.record_notified("dec-1") is False
    assert store.record_notified("dec-2") is True


def test_seen_set_survives_store_restart(tmp_path: Path) -> None:
    path = tmp_path / "seen.json"
    FactoryDecisionNotificationStore(path).record_notified("dec-1")

    restarted = FactoryDecisionNotificationStore(path)
    assert restarted.record_notified("dec-1") is False


def test_prune_drops_ids_no_longer_pending(tmp_path: Path) -> None:
    path = tmp_path / "seen.json"
    store = FactoryDecisionNotificationStore(path)
    store.record_notified("dec-1")
    store.record_notified("dec-2")

    store.prune(["dec-2"])

    assert store.record_notified("dec-2") is False
    assert store.record_notified("dec-1") is True


def test_corrupt_file_treated_as_empty(tmp_path: Path) -> None:
    path = tmp_path / "seen.json"
    path.write_text("{not json", encoding="utf-8")

    store = FactoryDecisionNotificationStore(path)
    assert store.record_notified("dec-1") is True


def test_non_list_payload_treated_as_empty(tmp_path: Path) -> None:
    path = tmp_path / "seen.json"
    path.write_text('{"dec-1": true}', encoding="utf-8")

    store = FactoryDecisionNotificationStore(path)
    assert store.record_notified("dec-1") is True
