# ========= 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 asyncio

import pytest

from mirage.accessor.discord import DiscordAccessor
from mirage.cache.index import IndexEntry
from mirage.cache.index.ram import RAMIndexCacheStore
from mirage.core.discord.stat import stat
from mirage.types import FileType, PathSpec


@pytest.fixture
def index():
    store = RAMIndexCacheStore()
    asyncio.run(
        store.put(
            "/My Server",
            IndexEntry(
                id="G001",
                name="My Server",
                resource_type="discord/guild",
                vfs_name="My Server",
            ),
        ))
    asyncio.run(
        store.put(
            "/My Server/channels/general",
            IndexEntry(
                id="C001",
                name="general",
                resource_type="discord/channel",
                vfs_name="general",
            ),
        ))
    asyncio.run(
        store.put(
            "/My Server/members/alice.json",
            IndexEntry(
                id="U001",
                name="alice",
                resource_type="discord/member",
                vfs_name="alice.json",
            ),
        ))
    return store


@pytest.fixture
def accessor():
    return DiscordAccessor(config=object())


@pytest.mark.asyncio
async def test_stat_root(accessor, index):
    result = await stat(accessor, PathSpec(original="/", directory="/"), index)
    assert result.type == FileType.DIRECTORY
    assert result.name == "/"


@pytest.mark.asyncio
async def test_stat_guild(accessor, index):
    result = await stat(
        accessor, PathSpec(original="/My Server", directory="/My Server"),
        index)
    assert result.type == FileType.DIRECTORY
    assert result.extra["guild_id"] == "G001"


@pytest.mark.asyncio
async def test_stat_channel(accessor, index):
    result = await stat(
        accessor,
        PathSpec(original="/My Server/channels/general",
                 directory="/My Server/channels/general"), index)
    assert result.type == FileType.DIRECTORY
    assert result.extra["channel_id"] == "C001"


@pytest.mark.asyncio
async def test_stat_member(accessor, index):
    result = await stat(
        accessor,
        PathSpec(original="/My Server/members/alice.json",
                 directory="/My Server/members/alice.json"), index)
    assert result.type == FileType.JSON
    assert result.extra["user_id"] == "U001"


@pytest.mark.asyncio
async def test_stat_jsonl(accessor, index):
    result = await stat(
        accessor,
        PathSpec(original="/My Server/channels/general/2024-01-15.jsonl",
                 directory="/My Server/channels/general/2024-01-15.jsonl"),
        index,
    )
    assert result.type == FileType.TEXT
    assert result.name == "2024-01-15.jsonl"


@pytest.mark.asyncio
async def test_stat_not_found(accessor, index):
    with pytest.raises(FileNotFoundError):
        await stat(
            accessor,
            PathSpec(original="/nonexistent/path",
                     directory="/nonexistent/path"), index)
