"""Shared test fixtures for axm-bib tests."""

from __future__ import annotations

import warnings
from collections.abc import Generator
from unittest.mock import patch

import pytest

from axm_bib.models import BibEntry, DownloadResult, Paper


@pytest.fixture(autouse=True)
def _no_http_retry_sleep() -> Generator[None, None, None]:
    """Prevent real time.sleep inside request_with_retry during tests."""
    with patch("axm_bib.http.time.sleep"):
        yield


def pytest_configure() -> None:
    """Pre-import pymupdf before pytest sets filterwarnings=error.

    Workaround for pymupdf issue #4392: pymupdf's C extension segfaults
    when any warning is raised as error during import.
    """
    with warnings.catch_warnings():
        warnings.simplefilter("default")
        import pymupdf  # noqa: F401


# ── Sample Data ──────────────────────────────────────────────────────────

SAMPLE_DOI = "10.1145/3292500.3330919"
SAMPLE_ARXIV_ID = "1706.03762"

SAMPLE_BIBTEX_RAW = (
    "@inproceedings{smith2024deep,\n"
    "  author = {Smith, John and Doe, Jane},\n"
    "  title = {Deep Learning for Citation Analysis},\n"
    "  year = {2024},\n"
    "  doi = {10.1145/3292500.3330919}\n"
    "}"
)

SAMPLE_CROSSREF_BIBTEX = (
    "@article{_2024,\n"
    "  author = {Smith, John and Doe, Jane},\n"
    "  title = {Deep Learning for Citation Analysis},\n"
    "  year = {2024},\n"
    "  doi = {10.1145/3292500.3330919},\n"
    "  journal = {Journal of ML}\n"
    "}"
)

SAMPLE_S2_PAPER = {
    "paperId": "abc123",
    "title": "Deep Learning for Citation Analysis",
    "authors": [{"name": "John Smith"}, {"name": "Jane Doe"}],
    "year": 2024,
    "externalIds": {"DOI": SAMPLE_DOI, "ArXiv": SAMPLE_ARXIV_ID},
    "abstract": "We present a deep learning approach...",
    "venue": "KDD",
    "citationCount": 42,
    "isOpenAccess": True,
    "openAccessPdf": {"url": "https://arxiv.org/pdf/1706.03762.pdf"},
}

SAMPLE_S2_PAPER_NO_DOI = {
    "paperId": "def456",
    "title": "Some Workshop Paper",
    "authors": [{"name": "Alice Bob"}],
    "year": 2023,
    "externalIds": {},
    "abstract": None,
    "venue": "Workshop",
    "citationCount": 0,
    "isOpenAccess": False,
    "openAccessPdf": None,
}

SAMPLE_UNPAYWALL_RESPONSE = {
    "doi": SAMPLE_DOI,
    "is_oa": True,
    "best_oa_location": {
        "url_for_pdf": "https://arxiv.org/pdf/1706.03762.pdf",
        "url": "https://arxiv.org/abs/1706.03762",
        "host_type": "repository",
    },
    "oa_locations": [
        {
            "url_for_pdf": "https://arxiv.org/pdf/1706.03762.pdf",
            "url": "https://arxiv.org/abs/1706.03762",
            "host_type": "repository",
        },
    ],
}

SAMPLE_UNPAYWALL_CLOSED = {
    "doi": "10.1234/closed.5678",
    "is_oa": False,
    "best_oa_location": None,
    "oa_locations": [],
}


# ── Fixtures ─────────────────────────────────────────────────────────────


@pytest.fixture
def sample_paper() -> Paper:
    """A fully populated Paper object."""
    return Paper(
        title="Deep Learning for Citation Analysis",
        authors=["John Smith", "Jane Doe"],
        year=2024,
        doi=SAMPLE_DOI,
        arxiv_id=SAMPLE_ARXIV_ID,
        abstract="We present a deep learning approach...",
        venue="KDD",
        citation_count=42,
        open_access_pdf="https://arxiv.org/pdf/1706.03762.pdf",
        source="semantic_scholar",
    )


@pytest.fixture
def sample_bib_entry() -> BibEntry:
    """A fully populated BibEntry object."""
    return BibEntry(
        key="smith2024deep",
        entry_type="inproceedings",
        raw=SAMPLE_BIBTEX_RAW,
        doi=SAMPLE_DOI,
        title="Deep Learning for Citation Analysis",
        authors=["John Smith", "Jane Doe"],
        year=2024,
    )


@pytest.fixture
def sample_download_result(tmp_path: object) -> DownloadResult:
    """A DownloadResult pointing to a temp file."""
    return DownloadResult(
        doi=SAMPLE_DOI,
        path="/tmp/papers/smith2024deep.pdf",
        size_bytes=1024,
        is_open_access=True,
    )
