"""The upload's scan breadth: typed by filing type, untyped by cover attribution."""

from __future__ import annotations

from pathlib import Path

import fitz
import pytest

from quber.agents.cover_attribution import CoverAttribution, MockCoverAttributor
from quber.playground.app import _scan_breadth


@pytest.fixture
def one_page_pdf(tmp_path: Path) -> Path:
    pdf = tmp_path / "untitled.pdf"
    doc = fitz.open()
    doc.new_page(width=720, height=540)
    doc.save(pdf)
    doc.close()
    return pdf


def test_typed_big_filing_stays_nominated(tmp_path: Path):
    pages, reason = _scan_breadth({"filing_type": "10-Q"}, tmp_path / "absent.pdf", tmp_path)
    assert pages == "nominated"
    assert "10-Q" in reason


def test_typed_filing_type_is_case_insensitive(tmp_path: Path):
    pages, _reason = _scan_breadth({"filing_type": "10-k"}, tmp_path / "absent.pdf", tmp_path)
    assert pages == "nominated"


def test_any_other_explicit_type_scans_all_pages(tmp_path: Path):
    pages, reason = _scan_breadth({"filing_type": "99-2"}, tmp_path / "absent.pdf", tmp_path)
    assert pages == "all"
    assert "99-2" in reason


def test_untyped_presentation_attribution_scans_all_pages(
    one_page_pdf: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
    from quber.agents import cover_attribution

    monkeypatch.setattr(
        cover_attribution,
        "get_cover_attributor",
        lambda: MockCoverAttributor(CoverAttribution(kind="presentation", reason="deck cover")),
    )
    pages, reason = _scan_breadth({"filing_type": None}, one_page_pdf, tmp_path)
    assert pages == "all"
    assert "presentation" in reason


def test_untyped_filing_attribution_stays_nominated(
    one_page_pdf: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
    from quber.agents import cover_attribution

    monkeypatch.setattr(
        cover_attribution,
        "get_cover_attributor",
        lambda: MockCoverAttributor(CoverAttribution(kind="filing", reason="form header")),
    )
    pages, _reason = _scan_breadth({}, one_page_pdf, tmp_path)
    assert pages == "nominated"


def test_attribution_failure_falls_back_to_nominated_and_says_so(
    one_page_pdf: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
    from quber.agents import cover_attribution

    class ExplodingAttributor:
        async def attribute(self, cover_png: bytes, page_count: int) -> CoverAttribution:
            raise RuntimeError("model unavailable")

    monkeypatch.setattr(cover_attribution, "get_cover_attributor", lambda: ExplodingAttributor())
    pages, reason = _scan_breadth({}, one_page_pdf, tmp_path)
    assert pages == "nominated"
    assert "attribution failed" in reason
    assert "fallback" in reason


def test_box_containment_predicate():
    from quber.playground.app import _box_contains

    panel = {"left": 0.05, "top": 0.34, "right": 0.58, "bottom": 0.70}
    value = {"left": 0.05, "top": 0.45, "right": 0.14, "bottom": 0.50}
    assert _box_contains(panel, value)
    assert not _box_contains(value, panel)
    # A box beside the value, not around it.
    beside = {"left": 0.60, "top": 0.45, "right": 0.90, "bottom": 0.50}
    assert not _box_contains(beside, value)
