"""Integration tests for list + permissions + zone filtering (Issue #904).

Wire up real SearchService + RustMetastoreProxy + OperationContext
to verify zone-scoped listing with permission filtering.
"""

import tempfile
from pathlib import Path
from typing import Any

from nexus.contracts.constants import ROOT_ZONE_ID  # noqa: F401 — kept for future use

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _make_store(zone_id: str = ROOT_ZONE_ID) -> Any:  # noqa: ARG001
    """Open a fresh ``PyKernel`` backed by a tempdir redb.

    ``zone_id`` is accepted for API compatibility — every call yields a
    separate kernel + redb so distinct stores are naturally isolated.
    """
    from nexus_runtime import PyKernel as _Kernel

    tmpdir = tempfile.mkdtemp()
    kernel = _Kernel()
    kernel.set_metastore_path(str(Path(tmpdir) / "meta.redb"))
    return kernel


def _make_search_service(
    store: Any,
    enforce_permissions: bool = False,
) -> Any:
    """Create a SearchService backed by the given store."""
    from nexus.bricks.search.search_service import SearchService

    return SearchService(
        metadata_store=store,
        enforce_permissions=enforce_permissions,
    )


# ===========================================================================
# Tests
# ===========================================================================


class TestListWithZoneAndPermissions:
    """Integration tests for list with zone filtering and permissions."""

    def test_list_returns_all_files_in_store(self) -> None:
        """Basic list returns all files in the store."""
        store = _make_store()
        try:
            store.sys_setattr("/file1.txt", 0)
            store.sys_setattr("/file2.txt", 0)
            store.sys_setattr("/dir/file3.txt", 0)

            svc = _make_search_service(store)
            results = svc.list(path="/", recursive=True)

            assert "/file1.txt" in results
            assert "/file2.txt" in results
            assert "/dir/file3.txt" in results
        finally:
            pass  # kernel manages redb lifecycle

    def test_list_non_recursive_excludes_nested(self) -> None:
        """Non-recursive list should exclude nested files."""
        store = _make_store()
        try:
            store.sys_setattr("/file1.txt", 0)
            store.sys_setattr("/dir/file2.txt", 0)
            store.sys_setattr("/dir/sub/file3.txt", 0)

            svc = _make_search_service(store)
            results = svc.list(path="/", recursive=False)

            assert "/file1.txt" in results
            # Nested files should not be in non-recursive results
            assert "/dir/file2.txt" not in results
            assert "/dir/sub/file3.txt" not in results
        finally:
            pass  # kernel manages redb lifecycle

    def test_list_with_prefix_filters(self) -> None:
        """List with prefix should only return matching files."""
        store = _make_store()
        try:
            store.sys_setattr("/a/file1.txt", 0)
            store.sys_setattr("/a/file2.txt", 0)
            store.sys_setattr("/b/file3.txt", 0)

            svc = _make_search_service(store)
            results = svc.list(path="/a/", recursive=True)

            assert "/a/file1.txt" in results
            assert "/a/file2.txt" in results
            assert "/b/file3.txt" not in results
        finally:
            pass  # kernel manages redb lifecycle

    def test_list_empty_store_returns_empty(self) -> None:
        """List on empty store returns empty."""
        store = _make_store()
        try:
            svc = _make_search_service(store)
            results = svc.list(path="/", recursive=True)
            assert results == []
        finally:
            pass  # kernel manages redb lifecycle

    def test_zone_local_store_isolation(self) -> None:
        """Two separate stores for different zones remain isolated."""
        store_a = _make_store(zone_id="zone_a")
        store_b = _make_store(zone_id="zone_b")
        try:
            store_a.sys_setattr("/zone_a/file1.txt", 0)
            store_b.sys_setattr("/zone_b/file2.txt", 0)

            svc_a = _make_search_service(store_a)
            svc_b = _make_search_service(store_b)

            results_a = svc_a.list(path="/", recursive=True)
            results_b = svc_b.list(path="/", recursive=True)

            assert "/zone_a/file1.txt" in results_a
            assert "/zone_b/file2.txt" not in results_a

            assert "/zone_b/file2.txt" in results_b
            assert "/zone_a/file1.txt" not in results_b
        finally:
            pass  # kernel manages redb lifecycle

    def test_list_paginated_returns_correct_pages(self) -> None:
        """Paginated list should return correct pages."""
        store = _make_store()
        try:
            for i in range(10):
                store.sys_setattr(f"/p/{chr(97 + i)}.txt", 0)

            svc = _make_search_service(store)
            page1 = svc._list_paginated(
                path="/p/", recursive=True, details=False, limit=3, cursor=None, context=None
            )

            assert len(page1.items) == 3
            assert page1.has_more is True
            assert page1.next_cursor is not None
        finally:
            pass  # kernel manages redb lifecycle

    def test_list_details_includes_metadata(self) -> None:
        """List with details=True should return dicts with metadata."""
        store = _make_store()
        try:
            store.sys_setattr("/doc.txt", 0)

            svc = _make_search_service(store)
            results = svc.list(path="/", recursive=True, details=True)

            assert len(results) >= 1
            doc = next((r for r in results if r["path"] == "/doc.txt"), None)
            assert doc is not None
            assert doc["size"] == 0
            assert doc["is_directory"] is False
        finally:
            pass  # kernel manages redb lifecycle
