"""Tests for PDFStorageManager._generate_filename() and _infer_storage_mode() edge cases.

Fills gaps NOT covered by test_pdf_storage_manager_edge_cases.py.
"""

from unittest.mock import Mock

import pytest

from local_deep_research.research_library.services.pdf_storage_manager import (
    PDFStorageManager,
)


@pytest.fixture()
def manager(tmp_path):
    """Create a PDFStorageManager rooted in a pytest-managed temp directory."""
    return PDFStorageManager(
        library_root=tmp_path,
        storage_mode="database",
        max_pdf_size_mb=100,
    )


# ---------------------------------------------------------------------------
# _generate_filename edge cases
# ---------------------------------------------------------------------------


class TestGenerateFilenameEdgeCases:
    """Edge cases not covered by test_pdf_storage_manager_edge_cases.py."""

    def test_arxiv_url_without_valid_id_fallback(self, manager):
        """arXiv URL with no matching ID pattern → fallback with timestamp."""
        result = manager._generate_filename(
            "https://arxiv.org/some/page", 42, "fallback.pdf"
        )
        assert result.startswith("arxiv_")
        assert "42" in result
        assert result.endswith(".pdf")

    def test_arxiv_url_resource_id_none(self, manager):
        """arXiv URL without valid ID and resource_id=None → 'unknown'."""
        result = manager._generate_filename(
            "https://arxiv.org/some/page", None, "fallback.pdf"
        )
        assert "unknown" in result
        assert result.startswith("arxiv_")

    def test_pubmed_url_without_pmc_id(self, manager):
        """PMC URL without PMC ID → fallback with timestamp."""
        result = manager._generate_filename(
            "https://ncbi.nlm.nih.gov/pmc/articles/", 99, "fallback.pdf"
        )
        assert result.startswith("pubmed_")
        assert "99" in result

    def test_pubmed_url_resource_id_none(self, manager):
        """PMC URL without PMC ID and resource_id=None → 'unknown'."""
        result = manager._generate_filename(
            "https://ncbi.nlm.nih.gov/pmc/articles/", None, "fallback.pdf"
        )
        assert "unknown" in result
        assert result.startswith("pubmed_")

    def test_url_with_no_hostname(self, manager):
        """URL with empty hostname → returns fallback."""
        result = manager._generate_filename(
            "file:///local/path.pdf", 1, "fallback.pdf"
        )
        # file:// has empty hostname → not arxiv/pmc → fallback
        assert result == "fallback.pdf"

    def test_arxiv_subdomain_without_valid_id(self, manager):
        """export.arxiv.org with no valid arXiv ID → fallback."""
        result = manager._generate_filename(
            "https://export.arxiv.org/noarxivid", 5, "fallback.pdf"
        )
        assert result.startswith("arxiv_")
        assert "5" in result

    def test_ncbi_non_pmc_path_returns_fallback(self, manager):
        """ncbi.nlm.nih.gov but not /pmc path → returns fallback."""
        result = manager._generate_filename(
            "https://ncbi.nlm.nih.gov/pubmed/12345", 1, "fallback.pdf"
        )
        # path doesn't contain "/pmc" → not matched → fallback
        assert result == "fallback.pdf"


# ---------------------------------------------------------------------------
# _infer_storage_mode edge cases
# ---------------------------------------------------------------------------


class TestInferStorageModeEdgeCases:
    """Edge cases not covered by test_pdf_storage_manager_edge_cases.py."""

    def test_document_no_blob_no_path_returns_none(self, manager):
        doc = Mock()
        doc.blob = None
        doc.file_path = None
        assert manager._infer_storage_mode(doc) == "none"

    def test_document_without_blob_attribute(self, manager):
        """Document that doesn't have blob attribute (no hasattr)."""
        doc = Mock(spec=[])  # No attributes at all
        doc.file_path = "pdfs/paper.pdf"
        # hasattr(doc, "blob") is False → skips blob check → checks file_path
        assert manager._infer_storage_mode(doc) == "filesystem"

    def test_document_without_blob_attr_no_path(self, manager):
        """Document without blob attribute AND no file_path."""
        doc = Mock(spec=[])
        doc.file_path = None
        assert manager._infer_storage_mode(doc) == "none"

    def test_document_empty_file_path_returns_none(self, manager):
        """Empty string file_path is falsy → returns 'none'."""
        doc = Mock()
        doc.blob = None
        doc.file_path = ""
        assert manager._infer_storage_mode(doc) == "none"

    def test_blob_takes_priority_over_file_path(self, manager):
        """When both blob and file_path exist, blob wins → 'database'."""
        doc = Mock()
        doc.blob = Mock()  # truthy
        doc.file_path = "pdfs/paper.pdf"
        assert manager._infer_storage_mode(doc) == "database"
