"""Tests for uncovered paths in local_deep_research.document_loaders.bytes_loader."""

from unittest.mock import Mock, patch

import pytest
from langchain_core.documents import Document


BYTES_LOADER = "local_deep_research.document_loaders.bytes_loader"


class TestLoadFromBytesUnsupportedExtension:
    @patch(f"{BYTES_LOADER}.is_extension_supported", return_value=False)
    def test_raises_value_error(self, _mock_supported):
        from local_deep_research.document_loaders.bytes_loader import (
            load_from_bytes,
        )

        with pytest.raises(ValueError, match="Unsupported file extension"):
            load_from_bytes(b"data", ".xyz", "file.xyz")


class TestLoadFromBytesLoaderInfoNone:
    @patch(f"{BYTES_LOADER}.get_loader_class_for_extension", return_value=None)
    @patch(f"{BYTES_LOADER}.is_extension_supported", return_value=True)
    def test_raises_value_error_no_loader(self, _mock_supported, _mock_get):
        from local_deep_research.document_loaders.bytes_loader import (
            load_from_bytes,
        )

        with pytest.raises(ValueError, match="No loader found for extension"):
            load_from_bytes(b"data", ".abc", "file.abc")


class TestLoadFromBytesSuccess:
    @patch(f"{BYTES_LOADER}.get_loader_class_for_extension")
    @patch(f"{BYTES_LOADER}.is_extension_supported", return_value=True)
    def test_metadata_original_filename(self, _mock_supported, mock_get_loader):
        mock_loader_cls = Mock()
        mock_loader_cls.return_value.load.return_value = [
            Document(page_content="hello")
        ]
        mock_get_loader.return_value = (mock_loader_cls, {})

        from local_deep_research.document_loaders.bytes_loader import (
            load_from_bytes,
        )

        docs = load_from_bytes(b"content", ".txt", "test.txt")
        assert len(docs) == 1
        assert docs[0].metadata["original_filename"] == "test.txt"

    @patch(f"{BYTES_LOADER}.get_loader_class_for_extension")
    @patch(f"{BYTES_LOADER}.is_extension_supported", return_value=True)
    def test_metadata_source_url(self, _mock_supported, mock_get_loader):
        mock_loader_cls = Mock()
        mock_loader_cls.return_value.load.return_value = [
            Document(page_content="hello")
        ]
        mock_get_loader.return_value = (mock_loader_cls, {})

        from local_deep_research.document_loaders.bytes_loader import (
            load_from_bytes,
        )

        docs = load_from_bytes(
            b"content", "txt", "test.txt", source_url="http://example.com"
        )
        assert docs[0].metadata["source_url"] == "http://example.com"

    @patch(f"{BYTES_LOADER}.get_loader_class_for_extension")
    @patch(f"{BYTES_LOADER}.is_extension_supported", return_value=True)
    def test_no_source_url_not_in_metadata(
        self, _mock_supported, mock_get_loader
    ):
        mock_loader_cls = Mock()
        mock_loader_cls.return_value.load.return_value = [
            Document(page_content="hello")
        ]
        mock_get_loader.return_value = (mock_loader_cls, {})

        from local_deep_research.document_loaders.bytes_loader import (
            load_from_bytes,
        )

        docs = load_from_bytes(b"content", ".txt", "test.txt")
        assert "source_url" not in docs[0].metadata


class TestExtractTextFromBytesUnsupported:
    @patch(f"{BYTES_LOADER}.is_extension_supported", return_value=False)
    def test_returns_none(self, _mock_supported):
        from local_deep_research.document_loaders.bytes_loader import (
            extract_text_from_bytes,
        )

        result = extract_text_from_bytes(b"data", ".xyz", "file.xyz")
        assert result is None


class TestExtractTextFromBytesLoaderFails:
    @patch(f"{BYTES_LOADER}.get_loader_class_for_extension")
    @patch(f"{BYTES_LOADER}.is_extension_supported", return_value=True)
    def test_returns_none_on_exception(self, _mock_supported, mock_get_loader):
        mock_loader_cls = Mock()
        mock_loader_cls.return_value.load.side_effect = RuntimeError("boom")
        mock_get_loader.return_value = (mock_loader_cls, {})

        from local_deep_research.document_loaders.bytes_loader import (
            extract_text_from_bytes,
        )

        result = extract_text_from_bytes(b"data", ".txt", "file.txt")
        assert result is None


class TestExtractTextFromBytesEmptyDocuments:
    @patch(f"{BYTES_LOADER}.get_loader_class_for_extension")
    @patch(f"{BYTES_LOADER}.is_extension_supported", return_value=True)
    def test_returns_none_for_empty_list(
        self, _mock_supported, mock_get_loader
    ):
        mock_loader_cls = Mock()
        mock_loader_cls.return_value.load.return_value = []
        mock_get_loader.return_value = (mock_loader_cls, {})

        from local_deep_research.document_loaders.bytes_loader import (
            extract_text_from_bytes,
        )

        result = extract_text_from_bytes(b"data", ".txt", "file.txt")
        assert result is None
