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

from unittest.mock import AsyncMock, patch

import pytest

from mirage.accessor.email import EmailAccessor
from mirage.cache.index.ram import RAMIndexCacheStore
from mirage.commands.builtin.email.find import find
from mirage.commands.config import CommandOpts
from mirage.core.email.config import EmailConfig
from mirage.types import PathSpec


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


def _accessor() -> EmailAccessor:
    return EmailAccessor(
        EmailConfig(imap_host="imap.test",
                    smtp_host="smtp.test",
                    username="u",
                    password="p"))


async def _run(paths, *texts: str, **flags) -> list[str]:
    with patch("mirage.core.email.readdir.list_folders",
               new_callable=AsyncMock,
               return_value=["INBOX", "Sent"]):
        stdout, _io = await find(
            _accessor(), paths, list(texts),
            CommandOpts(index=RAMIndexCacheStore(), flags={**flags}))
    data = stdout if isinstance(stdout, bytes) else b""
    return data.decode().splitlines()


@pytest.mark.asyncio
async def test_walk_lists_folders():
    lines = await _run([_spec("/")], maxdepth="1")
    assert "/INBOX" in lines
    assert "/Sent" in lines


@pytest.mark.asyncio
async def test_path_pattern_is_honored():
    lines = await _run([_spec("/")], maxdepth="1", path="*INBOX*")
    assert lines == ["/INBOX"]


@pytest.mark.asyncio
async def test_size_is_honored_dirs_count_as_zero():
    lines = await _run([_spec("/")], maxdepth="1", size="+0c")
    assert lines == []


ATTACHMENT_HEADERS = [{
    "uid":
    "7",
    "subject":
    "Report",
    "date":
    "Mon, 04 Aug 2026 10:00:00 +0000",
    "attachments": [{
        "filename": "invoice.pdf",
        "size": 123
    }],
}]


@pytest.mark.asyncio
async def test_type_f_lists_attachments():
    with patch("mirage.core.email.readdir.list_folders",
               new_callable=AsyncMock,
               return_value=["INBOX"]), \
         patch("mirage.core.email.readdir.list_message_uids",
               new_callable=AsyncMock,
               return_value=["7"]), \
         patch("mirage.core.email.readdir.fetch_headers",
               new_callable=AsyncMock,
               return_value=ATTACHMENT_HEADERS):
        stdout, _io = await find(
            _accessor(), [_spec('/')], [],
            CommandOpts(index=RAMIndexCacheStore(), flags={'type': 'f'}))
    lines = (stdout
             if isinstance(stdout, bytes) else b"").decode().splitlines()
    assert "/INBOX/2026-08-04/Report__7.email.json" in lines
    assert "/INBOX/2026-08-04/Report__7/invoice.pdf" in lines
    assert "/INBOX/2026-08-04/Report__7" not in lines


@pytest.mark.asyncio
async def test_type_d_lists_attachment_dir_not_attachment():
    with patch("mirage.core.email.readdir.list_folders",
               new_callable=AsyncMock,
               return_value=["INBOX"]), \
         patch("mirage.core.email.readdir.list_message_uids",
               new_callable=AsyncMock,
               return_value=["7"]), \
         patch("mirage.core.email.readdir.fetch_headers",
               new_callable=AsyncMock,
               return_value=ATTACHMENT_HEADERS):
        stdout, _io = await find(
            _accessor(), [_spec('/')], [],
            CommandOpts(index=RAMIndexCacheStore(), flags={'type': 'd'}))
    lines = (stdout
             if isinstance(stdout, bytes) else b"").decode().splitlines()
    assert "/INBOX/2026-08-04/Report__7" in lines
    assert "/INBOX/2026-08-04/Report__7/invoice.pdf" not in lines


@pytest.mark.asyncio
async def test_name_only_folder_level_pushes_down_to_imap_search():
    search = AsyncMock(return_value=[])
    with patch("mirage.commands.builtin.email.find.search_messages", search):
        stdout, _io = await find(
            _accessor(), [_spec('/INBOX')], [],
            CommandOpts(index=RAMIndexCacheStore(), flags={'name':
                                                           '*report*'}))
    search.assert_awaited_once()
    assert (stdout if isinstance(stdout, bytes) else b"") == b""


@pytest.mark.asyncio
async def test_name_with_size_falls_through_to_walk():
    # Any predicate beyond -name must not be dropped by the server-side
    # shortcut; the local walk applies all of them.
    search = AsyncMock(return_value=[])
    with patch("mirage.commands.builtin.email.find.search_messages", search), \
         patch("mirage.core.email.readdir.list_folders",
               new_callable=AsyncMock,
               return_value=["INBOX"]), \
         patch("mirage.core.email.readdir.list_message_uids",
               new_callable=AsyncMock,
               return_value=[]):
        stdout, _io = await find(
            _accessor(), [_spec('/INBOX')], [],
            CommandOpts(index=RAMIndexCacheStore(),
                        flags={
                            'name': '*report*',
                            'size': '+0c'
                        }))
    search.assert_not_awaited()
    assert (stdout if isinstance(stdout, bytes) else b"") == b""


@pytest.mark.asyncio
async def test_second_folder_operand_falls_through_to_walk():
    # The IMAP subject search answers for one folder, so the second operand
    # used to be dropped in silence: `find /INBOX /Sent -name '*x*'` searched
    # INBOX only.
    search = AsyncMock(return_value=[])
    with patch("mirage.commands.builtin.email.find.search_messages", search), \
         patch("mirage.core.email.readdir.list_folders",
               new_callable=AsyncMock,
               return_value=["INBOX", "Sent"]), \
         patch("mirage.core.email.readdir.list_message_uids",
               new_callable=AsyncMock,
               return_value=[]):
        await find(
            _accessor(), [_spec('/INBOX'), _spec('/Sent')], [],
            CommandOpts(index=RAMIndexCacheStore(), flags={'name':
                                                           '*report*'}))
    search.assert_not_awaited()


@pytest.mark.asyncio
async def test_mount_root_walks_instead_of_reporting_no_match():
    # The root reached the push-down too, where `extract_folder` returned
    # None and it answered with an empty result and exit 0 — "nothing
    # matched" for a search it never ran. It has to walk.
    search = AsyncMock(return_value=[])
    with patch("mirage.commands.builtin.email.find.search_messages", search), \
         patch("mirage.core.email.readdir.list_folders",
               new_callable=AsyncMock,
               return_value=["INBOX", "Sent"]), \
         patch("mirage.core.email.readdir.list_message_uids",
               new_callable=AsyncMock,
               return_value=[]):
        stdout, _io = await find(
            _accessor(), [_spec('/')], [],
            CommandOpts(index=RAMIndexCacheStore(), flags={'name': 'Sent'}))
    search.assert_not_awaited()
    lines = (stdout if isinstance(stdout, bytes) else b"").decode().split()
    assert "/Sent" in lines
