from __future__ import annotations

import asyncio
import sys
import tempfile
import unittest
from pathlib import Path

import httpx

sys.path.insert(0, str(Path(__file__).resolve().parents[3]))

from backend.app.dependencies import get_search_service
from backend.app.fs.filesystem_adapter import FilesystemAdapter
from backend.app.main import app
from backend.app.security.path_guard import PathGuard
from backend.app.services.search_service import SearchService


class SearchApiGoldenTest(unittest.TestCase):
    def setUp(self) -> None:
        self.temp_dir = tempfile.TemporaryDirectory()
        self.root = Path(self.temp_dir.name) / "root"
        self.root.mkdir(parents=True, exist_ok=True)
        self.service = SearchService(path_guard=PathGuard({"storage1": str(self.root)}), filesystem=FilesystemAdapter())

        async def _override_search_service() -> SearchService:
            return self.service

        app.dependency_overrides[get_search_service] = _override_search_service

    def tearDown(self) -> None:
        app.dependency_overrides.clear()
        self.temp_dir.cleanup()

    def _request(self, path: str, query: str) -> httpx.Response:
        async def _run() -> httpx.Response:
            transport = httpx.ASGITransport(app=app)
            async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
                return await client.get("/api/search", params={"path": path, "query": query})

        return asyncio.run(_run())

    def test_search_empty_result_list(self) -> None:
        (self.root / "docs").mkdir()
        (self.root / "docs" / "alpha.txt").write_text("a", encoding="utf-8")

        response = self._request("storage1/docs", "zzz")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {"items": [], "truncated": False})

    def test_search_file_match(self) -> None:
        (self.root / "docs").mkdir()
        (self.root / "docs" / "holiday-video.mp4").write_bytes(b"x")

        response = self._request("storage1/docs", "video")

        self.assertEqual(response.status_code, 200)
        payload = response.json()
        self.assertEqual(payload["truncated"], False)
        self.assertEqual(len(payload["items"]), 1)
        self.assertEqual(
            payload["items"][0],
            {
                "name": "holiday-video.mp4",
                "path": "storage1/docs/holiday-video.mp4",
                "type": "file",
                "parent_path": "storage1/docs",
                "root": "storage1",
            },
        )

    def test_search_directory_match(self) -> None:
        (self.root / "Projects").mkdir()
        (self.root / "Projects" / "DemoFolder").mkdir()

        response = self._request("storage1/Projects", "demo")

        self.assertEqual(response.status_code, 200)
        payload = response.json()
        self.assertEqual(payload["items"][0]["type"], "directory")
        self.assertEqual(payload["items"][0]["path"], "storage1/Projects/DemoFolder")

    def test_search_traversal_blocked(self) -> None:
        response = self._request("storage1/../etc", "passwd")

        self.assertEqual(response.status_code, 403)
        self.assertEqual(response.json()["error"]["code"], "path_traversal_detected")

    def test_search_path_not_found(self) -> None:
        response = self._request("storage1/missing", "abc")

        self.assertEqual(response.status_code, 404)
        self.assertEqual(response.json()["error"]["code"], "path_not_found")

    def test_search_invalid_root_alias(self) -> None:
        response = self._request("unknown/path", "abc")

        self.assertEqual(response.status_code, 403)
        self.assertEqual(response.json()["error"]["code"], "invalid_root_alias")

    def test_search_result_limit_sets_truncated(self) -> None:
        (self.root / "many").mkdir()
        for idx in range(120):
            (self.root / "many" / f"match-{idx:03d}.txt").write_text("x", encoding="utf-8")

        response = self._request("storage1/many", "match")

        self.assertEqual(response.status_code, 200)
        payload = response.json()
        self.assertEqual(len(payload["items"]), 100)
        self.assertEqual(payload["truncated"], True)


if __name__ == "__main__":
    unittest.main()
