from pathlib import Path
import os

os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")

from PySide6.QtWidgets import QApplication, QWidget

from awakening_widgets import ItemCardOverlay
from image_repository import ImageRepository


def app():
    return QApplication.instance() or QApplication([])


def test_item_card_is_in_window_overlay(tmp_path):
    _app = app()
    parent = QWidget()
    parent.resize(1000, 700)
    parent.show()
    _app.processEvents()
    card = ItemCardOverlay(ImageRepository(tmp_path / "cache"), parent)
    card.show_item(
        token="x", title="Testowy przedmiot", details=[("Poziom", "Dobry +1")],
        category="head", base="Hełm", base_key="helm",
    )
    assert card.parent() is parent
    assert not card.isWindow()
    assert card.isVisible()
    card.close_card()
    assert not card.isVisible()


def test_initial_asset_report_mentions_unowned_ring_prefixes():
    report = (Path(__file__).resolve().parents[1] / "ASSET_COVERAGE_INITIAL.txt").read_text(encoding="utf-8")
    assert "Jastrzębi" in report
    assert "Pajęczy" in report
    assert "Słoneczny" in report


def test_image_repository_accepts_semantic_registry(tmp_path, monkeypatch):
    _app = app()
    # The packaged source already contains a registry. This test ensures loading it is harmless.
    repo = ImageRepository(tmp_path / "cache")
    assert isinstance(repo.registry, dict)
