"""Tests for storage factory."""

from unittest.mock import patch

import pytest

from local_deep_research.storage.factory import (
    get_report_storage,
)
from local_deep_research.storage.database_with_file_backup import (
    DatabaseWithFileBackupStorage,
)
from local_deep_research.config.thread_settings import NoSettingsContextError


class TestGetReportStorage:
    """Tests for get_report_storage factory function."""

    def test_raises_when_session_is_none(self):
        """Should raise ValueError when session is None."""
        with pytest.raises(ValueError, match="Database session is required"):
            get_report_storage(session=None)

    def test_returns_database_with_file_backup_storage(self, mock_session):
        """Should return DatabaseWithFileBackupStorage instance."""
        with patch(
            "local_deep_research.storage.factory.get_setting_from_snapshot"
        ) as mock_get:
            mock_get.return_value = False
            storage = get_report_storage(session=mock_session)

        assert isinstance(storage, DatabaseWithFileBackupStorage)

    def test_uses_provided_enable_file_backup_true(self, mock_session):
        """Should use enable_file_backup=True when explicitly provided."""
        storage = get_report_storage(
            session=mock_session, enable_file_backup=True
        )

        assert storage.enable_file_storage is True

    def test_uses_provided_enable_file_backup_false(self, mock_session):
        """Should use enable_file_backup=False when explicitly provided."""
        storage = get_report_storage(
            session=mock_session, enable_file_backup=False
        )

        assert storage.enable_file_storage is False

    def test_reads_setting_when_enable_file_backup_is_none(self, mock_session):
        """Should read from settings when enable_file_backup not provided."""
        with patch(
            "local_deep_research.storage.factory.get_setting_from_snapshot"
        ) as mock_get:
            mock_get.return_value = True
            storage = get_report_storage(session=mock_session)

        mock_get.assert_called_once_with(
            "report.enable_file_backup",
            settings_snapshot=None,
        )
        assert storage.enable_file_storage is True

    def test_passes_settings_snapshot_to_get_setting(self, mock_session):
        """Should pass settings_snapshot to get_setting_from_snapshot."""
        snapshot = {"report": {"enable_file_backup": True}}

        with patch(
            "local_deep_research.storage.factory.get_setting_from_snapshot"
        ) as mock_get:
            mock_get.return_value = True
            get_report_storage(session=mock_session, settings_snapshot=snapshot)

        mock_get.assert_called_once_with(
            "report.enable_file_backup",
            settings_snapshot=snapshot,
        )

    def test_defaults_to_false_when_no_settings_context(self, mock_session):
        """Should default to False when NoSettingsContextError raised."""
        with patch(
            "local_deep_research.storage.factory.get_setting_from_snapshot"
        ) as mock_get:
            mock_get.side_effect = NoSettingsContextError("No context")
            storage = get_report_storage(session=mock_session)

        assert storage.enable_file_storage is False
