from __future__ import annotations

import pytest

from ocr_pipeline.citations.regex_extractor import Citation, RegexCitationExtractor


def _isbns(text: str) -> list[str]:
    return [c.identifier for c in RegexCitationExtractor.extract(text) if c.type == "isbn"]


@pytest.mark.parametrize(
    ("text", "expected"),
    [
        ("ISBN:978-0-306-40615-7", "ISBN:9780306406157"),
        ("ISBN 978-0-306-40615-7", "ISBN:9780306406157"),
        ("ISBN-13: 978-0-306-40615-7", "ISBN:9780306406157"),
        ("ISBN:0-306-40615-2", "ISBN:0306406152"),
        ("ISBN 0-306-40615-2", "ISBN:0306406152"),
        ("ISBN-10: 0-306-40615-2", "ISBN:0306406152"),
    ],
)
def test_recognises_real_isbns(text: str, expected: str) -> None:
    assert expected in _isbns(text)


@pytest.mark.parametrize(
    "text",
    [
        "2024-01-15",
        "Due in 17 days, 2024",
        "https://www.jobbnorge.no/jobs/300588",
        "https://euraxess.ec.europa.eu/jobs/432300",
        "Anti-BRCA1 (1:1000), Anti-beta-actin (1:5000)",
        "30 ug per lane; gel 10% SDS-PAGE",
        "Lane 1: WT control",
        "PMID: 12345678",
        "DOI: 10.1038/nature12345",
    ],
)
def test_no_isbn_false_positives(text: str) -> None:
    assert _isbns(text) == []


def test_isbn_stripped_to_digits() -> None:
    identifiers = _isbns("ISBN: 978-0-306-40615-7")
    assert identifiers == ["ISBN:9780306406157"]


def test_doi_still_works() -> None:
    dois = [
        c for c in RegexCitationExtractor.extract("See DOI: 10.1038/nature12345") if c.type == "doi"
    ]
    assert len(dois) == 1
    assert dois[0].identifier == "10.1038/nature12345"


def test_pmid_still_works() -> None:
    pmids = [c for c in RegexCitationExtractor.extract("PMID: 12345678") if c.type == "pmid"]
    assert len(pmids) == 1
    assert pmids[0].identifier == "PMID:12345678"


def test_citation_dataclass_fields() -> None:
    c = Citation(raw="x", type="doi", identifier="y", confidence=0.5)
    assert c.context == ""
