# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========

import re
from datetime import datetime, timezone
from unittest.mock import patch

import pytest
from bson import ObjectId

from mirage.accessor.gridfs import GridFSAccessor, GridFSConfig
from mirage.cache.index.ram import RAMIndexCacheStore
from mirage.core.gridfs.readdir import readdir
from mirage.types import PathSpec

_UPLOAD = datetime(2026, 1, 1, tzinfo=timezone.utc)


@pytest.fixture
def accessor():
    return GridFSAccessor(config=GridFSConfig(
        uri="mongodb://localhost:27017", database="db", bucket="data"))


def _doc(filename: str, length: int = 0) -> dict:
    return {
        "filename": filename,
        "_id": ObjectId(),
        "length": length,
        "uploadDate": _UPLOAD,
    }


def _fake_iter(docs):

    async def iter_latest(accessor, query):
        for doc in docs:
            yield doc

    return iter_latest


def _path(s: str) -> PathSpec:
    return PathSpec(virtual=s, directory=s, resource_path=s.strip("/"))


@pytest.mark.asyncio
async def test_readdir_derives_files_dirs_and_markers(accessor):
    docs = [
        _doc("a.txt", 3),
        _doc("empty/"),
        _doc("sub/b.csv", 7),
        _doc("sub/deep/c.txt", 1),
    ]
    with patch("mirage.core.gridfs.driver.iter_latest", new=_fake_iter(docs)):
        entries = await readdir(accessor, _path("/"))
    assert entries == ["/a.txt", "/empty", "/sub"]


@pytest.mark.asyncio
async def test_readdir_skips_own_marker_and_dedupes(accessor):
    docs = [
        _doc("sub/"),
        _doc("sub/b.csv", 7),
        _doc("sub/deep/c.txt", 1),
        _doc("sub/deep/d.txt", 1),
    ]
    with patch("mirage.core.gridfs.driver.iter_latest", new=_fake_iter(docs)):
        entries = await readdir(accessor, _path("/sub"))
    assert entries == ["/sub/b.csv", "/sub/deep"]


@pytest.mark.asyncio
async def test_readdir_populates_index(accessor):
    index = RAMIndexCacheStore()
    docs = [_doc("a.txt", 3), _doc("sub/b.csv", 7)]
    with patch("mirage.core.gridfs.driver.iter_latest", new=_fake_iter(docs)):
        await readdir(accessor, _path("/"), index)
    lookup = await index.get("/a.txt")
    assert lookup.entry is not None
    assert lookup.entry.size == 3
    folder = await index.get("/sub")
    assert folder.entry is not None


class _FakeColl:

    def __init__(self, names: list[str]) -> None:
        self._names = names

    async def find_one(self, query, projection=None):
        del projection
        selector = query.get("filename")
        if isinstance(selector, str):
            match = selector in self._names
        else:
            pattern = re.compile(selector["$regex"])
            match = any(pattern.match(n) for n in self._names)
        return {"_id": ObjectId()} if match else None


def _fake_prefix_iter(names: list[str]):

    async def iter_latest(accessor, query):
        pattern = re.compile(query["filename"]["$regex"]) if query else None
        for name in names:
            if pattern is None or pattern.match(name):
                yield _doc(name)

    return iter_latest


def _latest_of(names: list[str]):

    async def latest_file(accessor, key):
        return {"_id": ObjectId(), "length": 0} if key in names else None

    return latest_file


def _bucket(names: list[str]):
    return patch.multiple(
        "mirage.core.gridfs.driver",
        iter_latest=_fake_prefix_iter(names),
        files_coll=lambda accessor: _FakeColl(names),
        latest_file=_latest_of(names),
    )


_NAMES = ["a.txt", "dir/f.txt", "dir/sub/g.txt", "empty/"]


@pytest.mark.asyncio
async def test_readdir_marker_only_directory_is_empty_not_missing(accessor):
    # The zero-length "empty/" marker doc is the only trace an empty
    # directory leaves, and it must read as an empty listing, not ENOENT.
    with _bucket(_NAMES):
        assert await readdir(accessor, _path("/empty")) == []


@pytest.mark.asyncio
async def test_readdir_root_of_an_empty_bucket_does_not_raise(accessor):
    with _bucket([]):
        assert await readdir(accessor, _path("/")) == []


@pytest.mark.asyncio
async def test_readdir_missing_path_is_enoent(accessor):
    with _bucket(_NAMES), pytest.raises(FileNotFoundError):
        await readdir(accessor, _path("/never.txt"))


@pytest.mark.asyncio
async def test_readdir_missing_nested_path_is_enoent(accessor):
    with _bucket(_NAMES), pytest.raises(FileNotFoundError):
        await readdir(accessor, _path("/nodir/deep"))


@pytest.mark.asyncio
async def test_readdir_on_a_file_doc_is_enotdir(accessor):
    with _bucket(_NAMES), pytest.raises(NotADirectoryError):
        await readdir(accessor, _path("/a.txt"))


@pytest.mark.asyncio
async def test_readdir_below_a_file_doc_is_enotdir(accessor):
    with _bucket(_NAMES), pytest.raises(NotADirectoryError):
        await readdir(accessor, _path("/a.txt/x"))


@pytest.mark.asyncio
async def test_readdir_missing_path_under_a_key_prefix_is_enoent():
    prefixed = GridFSAccessor(
        config=GridFSConfig(uri="mongodb://localhost:27017",
                            database="db",
                            bucket="data",
                            key_prefix="team"))
    with _bucket(["team/dir/f.txt"]), pytest.raises(FileNotFoundError):
        await readdir(prefixed, _path("/never.txt"))


@pytest.mark.asyncio
async def test_readdir_missing_child_of_a_coexisting_doc_and_prefix(accessor):
    # GridFS allows a file "a" and deeper files under "a/" at once, and a
    # child path reaches "a" only through the prefix, so a missing child is
    # ENOENT rather than ENOTDIR.
    with _bucket(["a", "a/x.txt"]), pytest.raises(FileNotFoundError):
        await readdir(accessor, _path("/a/never"))


@pytest.mark.asyncio
async def test_readdir_coexisting_prefix_still_lists_its_children(accessor):
    with _bucket(["a", "a/x.txt"]):
        assert await readdir(accessor, _path("/a")) == ["/a/x.txt"]
