"""Tests for index_local_library and view_document_chunks in rag_routes.py."""

from unittest.mock import patch, MagicMock

RAG_PREFIX = "/library"


class TestIndexLocalLibrary:
    """Tests for /api/rag/index-local SSE endpoint."""

    def test_requires_authentication(self, client):
        response = client.get(
            f"{RAG_PREFIX}/api/rag/index-local?path=/tmp/docs"
        )
        assert response.status_code in [401, 302]

    def test_missing_path_returns_400(self, authenticated_client):
        response = authenticated_client.get(f"{RAG_PREFIX}/api/rag/index-local")
        assert response.status_code == 400
        data = response.get_json()
        assert data["success"] is False
        assert "path" in data["error"].lower()

    @patch(
        "local_deep_research.research_library.routes.rag_routes.PathValidator"
    )
    def test_invalid_path_returns_400(
        self, mock_validator, authenticated_client
    ):
        mock_validator.validate_local_filesystem_path.side_effect = ValueError(
            "blocked"
        )

        response = authenticated_client.get(
            f"{RAG_PREFIX}/api/rag/index-local?path=/etc/passwd"
        )
        assert response.status_code == 400
        data = response.get_json()
        assert data["success"] is False

    @patch(
        "local_deep_research.research_library.routes.rag_routes.PathValidator"
    )
    def test_nonexistent_path_returns_400(
        self, mock_validator, authenticated_client, tmp_path
    ):
        nonexistent = tmp_path / "nonexistent"
        mock_validator.validate_local_filesystem_path.return_value = nonexistent
        mock_validator.sanitize_for_filesystem_ops.return_value = nonexistent

        response = authenticated_client.get(
            f"{RAG_PREFIX}/api/rag/index-local?path={nonexistent}"
        )
        assert response.status_code == 400

    @patch(
        "local_deep_research.research_library.routes.rag_routes.PathValidator"
    )
    def test_file_not_directory_returns_400(
        self, mock_validator, authenticated_client, tmp_path
    ):
        file_path = tmp_path / "file.txt"
        file_path.write_text("content")
        mock_validator.validate_local_filesystem_path.return_value = file_path
        mock_validator.sanitize_for_filesystem_ops.return_value = file_path

        response = authenticated_client.get(
            f"{RAG_PREFIX}/api/rag/index-local?path={file_path}"
        )
        assert response.status_code == 400


class TestViewDocumentChunks:
    """Tests for /api/rag/document/<id>/chunks endpoint."""

    def test_requires_authentication(self, client):
        response = client.get(f"{RAG_PREFIX}/document/123/chunks")
        assert response.status_code in [401, 302]

    @patch("local_deep_research.database.session_context.get_user_db_session")
    def test_document_not_found(self, mock_session_ctx, authenticated_client):
        mock_session = MagicMock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = None
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)

        response = authenticated_client.get(f"{RAG_PREFIX}/document/999/chunks")
        assert response.status_code == 404
