from __future__ import annotations

import asyncio
import errno
import sys
import tempfile
import threading
import time
import unittest
from pathlib import Path

import httpx

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

from backend.app.dependencies import get_move_task_service, get_task_service
from backend.app.db.task_repository import TaskRepository
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.move_task_service import MoveTaskService
from backend.app.services.task_service import TaskService
from backend.app.tasks_runner import TaskRunner


class FailingDeleteFilesystemAdapter(FilesystemAdapter):
    def delete_file(self, path: Path) -> None:
        raise OSError("forced delete failure")


class FailingBatchFilesystemAdapter(FilesystemAdapter):
    def move_file(self, source: str, destination: str) -> None:
        source_path = Path(source)
        if source_path.name == "fail-file.txt" or "fail-dir" in source_path.parts:
            raise OSError("forced batch move failure")
        super().move_file(source, destination)

    def move_directory(self, source: str, destination: str) -> None:
        if Path(source).name == "fail-dir":
            raise OSError("forced batch move failure")
        super().move_directory(source, destination)


class BlockingMoveFilesystemAdapter(FilesystemAdapter):
    def __init__(self) -> None:
        super().__init__()
        self.entered = threading.Event()
        self.release = threading.Event()

    def move_file(self, source: str, destination: str) -> None:
        self.entered.set()
        self.release.wait(timeout=2.0)
        super().move_file(source, destination)


class CrossDeviceMoveFilesystemAdapter(FilesystemAdapter):
    def move_file(self, source: str, destination: str) -> None:
        raise OSError(errno.EXDEV, "Invalid cross-device link")


class MoveApiGoldenTest(unittest.TestCase):
    def setUp(self) -> None:
        self.temp_dir = tempfile.TemporaryDirectory()
        self.root1 = Path(self.temp_dir.name) / "root1"
        self.root2 = Path(self.temp_dir.name) / "root2"
        self.root1.mkdir(parents=True, exist_ok=True)
        self.root2.mkdir(parents=True, exist_ok=True)

        self.repo = TaskRepository(str(Path(self.temp_dir.name) / "tasks.db"))
        path_guard = PathGuard({"storage1": str(self.root1), "storage2": str(self.root2)})
        self._set_services(path_guard=path_guard, filesystem=FilesystemAdapter())

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

    def _set_services(self, path_guard: PathGuard, filesystem: FilesystemAdapter) -> None:
        runner = TaskRunner(repository=self.repo, filesystem=filesystem)
        move_service = MoveTaskService(path_guard=path_guard, repository=self.repo, runner=runner)
        task_service = TaskService(repository=self.repo)

        async def _override_move_service() -> MoveTaskService:
            return move_service

        async def _override_task_service() -> TaskService:
            return task_service

        app.dependency_overrides[get_move_task_service] = _override_move_service
        app.dependency_overrides[get_task_service] = _override_task_service

    def _request(self, method: str, url: str, payload: dict | None = None) -> httpx.Response:
        async def _run() -> httpx.Response:
            transport = httpx.ASGITransport(app=app)
            async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
                if method == "POST":
                    return await client.post(url, json=payload)
                return await client.get(url)

        return asyncio.run(_run())

    def _wait_task(self, task_id: str, timeout_s: float = 2.0) -> dict:
        deadline = time.time() + timeout_s
        while time.time() < deadline:
            response = self._request("GET", f"/api/tasks/{task_id}")
            body = response.json()
            if body["status"] in {"completed", "failed", "cancelled"}:
                return body
            time.sleep(0.02)
        self.fail("task did not reach terminal state in time")

    def _wait_for_status(self, task_id: str, statuses: set[str], timeout_s: float = 2.0) -> dict:
        deadline = time.time() + timeout_s
        while time.time() < deadline:
            response = self._request("GET", f"/api/tasks/{task_id}")
            body = response.json()
            if body["status"] in statuses:
                return body
            time.sleep(0.02)
        self.fail(f"task did not reach one of {sorted(statuses)} in time")

    def test_move_success_same_root_create_task_shape_and_completed(self) -> None:
        src = self.root1 / "source.txt"
        src.write_text("hello", encoding="utf-8")

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source.txt", "destination": "storage1/moved.txt"},
        )

        self.assertEqual(response.status_code, 202)
        body = response.json()
        self.assertIn("task_id", body)
        self.assertEqual(body["status"], "queued")

        detail = self._wait_task(body["task_id"])
        self.assertEqual(detail["status"], "completed")
        self.assertEqual(detail["done_items"], 1)
        self.assertEqual(detail["total_items"], 1)
        self.assertTrue((self.root1 / "moved.txt").exists())
        self.assertFalse(src.exists())

    def test_move_directory_success_same_root_and_completed(self) -> None:
        src_dir = self.root1 / "source-dir"
        src_dir.mkdir()
        (src_dir / "nested.txt").write_text("hello", encoding="utf-8")
        target_parent = self.root1 / "target-parent"
        target_parent.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source-dir", "destination": "storage1/target-parent/moved-dir"},
        )

        self.assertEqual(response.status_code, 202)
        body = response.json()
        self.assertEqual(body["status"], "queued")

        detail = self._wait_task(body["task_id"])
        self.assertEqual(detail["status"], "completed")
        self.assertEqual(detail["done_items"], 1)
        self.assertEqual(detail["total_items"], 1)
        self.assertIsNone(detail["done_bytes"])
        self.assertIsNone(detail["total_bytes"])
        self.assertTrue((self.root1 / "target-parent" / "moved-dir").is_dir())
        self.assertTrue((self.root1 / "target-parent" / "moved-dir" / "nested.txt").exists())
        self.assertFalse(src_dir.exists())

    def test_move_directory_success_same_root_with_nested_symlink_keeps_direct_move_semantics(self) -> None:
        src_dir = self.root1 / "source-dir"
        src_dir.mkdir()
        real_dir = self.root1 / "real-dir"
        real_dir.mkdir()
        (real_dir / "nested.txt").write_text("hello", encoding="utf-8")
        (src_dir / "link-dir").symlink_to(real_dir, target_is_directory=True)
        target_parent = self.root1 / "target-parent"
        target_parent.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source-dir", "destination": "storage1/target-parent/moved-dir"},
        )

        self.assertEqual(response.status_code, 202)
        detail = self._wait_task(response.json()["task_id"])
        self.assertEqual(detail["status"], "completed")
        self.assertEqual(detail["done_items"], 1)
        self.assertEqual(detail["total_items"], 1)
        self.assertTrue((self.root1 / "target-parent" / "moved-dir").is_dir())
        self.assertTrue((self.root1 / "target-parent" / "moved-dir" / "link-dir").is_symlink())
        self.assertFalse(src_dir.exists())

    def test_move_success_cross_root_create_task_shape_and_completed(self) -> None:
        src = self.root1 / "source.txt"
        src.write_text("hello", encoding="utf-8")

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source.txt", "destination": "storage2/moved.txt"},
        )

        self.assertEqual(response.status_code, 202)
        body = response.json()
        self.assertIn("task_id", body)
        self.assertEqual(body["status"], "queued")

        detail = self._wait_task(body["task_id"])
        self.assertEqual(detail["status"], "completed")
        self.assertTrue((self.root2 / "moved.txt").exists())
        self.assertFalse(src.exists())

    def test_move_directory_cross_root_blocked(self) -> None:
        src_dir = self.root1 / "source-dir"
        src_dir.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source-dir", "destination": "storage2/source-dir"},
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()["error"]["code"], "invalid_request")

    def test_move_batch_same_root_directories_success(self) -> None:
        first = self.root1 / "first-dir"
        second = self.root1 / "second-dir"
        first.mkdir()
        second.mkdir()
        (first / "a.txt").write_text("a", encoding="utf-8")
        (second / "b.txt").write_text("b", encoding="utf-8")
        target = self.root1 / "target"
        target.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/first-dir", "storage1/second-dir"],
                "destination_base": "storage1/target",
            },
        )

        self.assertEqual(response.status_code, 202)
        detail = self._wait_task(response.json()["task_id"])
        self.assertEqual(detail["status"], "completed")
        self.assertEqual(detail["done_items"], 2)
        self.assertEqual(detail["total_items"], 2)
        self.assertTrue((target / "first-dir").is_dir())
        self.assertTrue((target / "second-dir").is_dir())
        self.assertFalse(first.exists())
        self.assertFalse(second.exists())

    def test_move_batch_same_root_mixed_files_and_directories_success(self) -> None:
        source_file = self.root1 / "one.txt"
        source_file.write_text("x", encoding="utf-8")
        source_dir = self.root1 / "dir-a"
        source_dir.mkdir()
        (source_dir / "nested.txt").write_text("y", encoding="utf-8")
        target = self.root1 / "target"
        target.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/one.txt", "storage1/dir-a"],
                "destination_base": "storage1/target",
            },
        )

        self.assertEqual(response.status_code, 202)
        detail = self._wait_task(response.json()["task_id"])
        self.assertEqual(detail["status"], "completed")
        self.assertEqual(detail["done_items"], 2)
        self.assertEqual(detail["total_items"], 2)
        self.assertTrue((target / "one.txt").exists())
        self.assertTrue((target / "dir-a").is_dir())
        self.assertFalse(source_file.exists())
        self.assertFalse(source_dir.exists())

    def test_move_batch_cancelled_after_current_file_finishes(self) -> None:
        blocking_fs = BlockingMoveFilesystemAdapter()
        path_guard = PathGuard({"storage1": str(self.root1), "storage2": str(self.root2)})
        self._set_services(path_guard=path_guard, filesystem=blocking_fs)
        (self.root1 / "a.txt").write_text("A", encoding="utf-8")
        (self.root1 / "b.txt").write_text("B", encoding="utf-8")
        target = self.root1 / "target"
        target.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/a.txt", "storage1/b.txt"],
                "destination_base": "storage1/target",
            },
        )

        task_id = response.json()["task_id"]
        self.assertTrue(blocking_fs.entered.wait(timeout=2.0))
        running = self._wait_for_status(task_id, {"running"})
        self.assertEqual(running["current_item"], "a.txt")

        cancel_response = self._request("POST", f"/api/tasks/{task_id}/cancel")
        self.assertEqual(cancel_response.status_code, 200)
        self.assertEqual(cancel_response.json()["status"], "cancelling")

        blocking_fs.release.set()
        detail = self._wait_task(task_id)
        self.assertEqual(detail["status"], "cancelled")
        self.assertEqual(detail["done_items"], 1)
        self.assertEqual(detail["total_items"], 2)
        self.assertTrue((target / "a.txt").exists())
        self.assertTrue((self.root1 / "b.txt").exists())
        self.assertFalse((target / "b.txt").exists())

    def test_move_batch_cross_root_files_success(self) -> None:
        first = self.root1 / "first.txt"
        second = self.root1 / "second.txt"
        first.write_text("a", encoding="utf-8")
        second.write_text("b", encoding="utf-8")

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/first.txt", "storage1/second.txt"],
                "destination_base": "storage2",
            },
        )

        self.assertEqual(response.status_code, 202)
        detail = self._wait_task(response.json()["task_id"])
        self.assertEqual(detail["status"], "completed")
        self.assertEqual(detail["done_items"], 2)
        self.assertEqual(detail["total_items"], 2)
        self.assertTrue((self.root2 / "first.txt").exists())
        self.assertTrue((self.root2 / "second.txt").exists())
        self.assertFalse(first.exists())
        self.assertFalse(second.exists())

    def test_move_batch_cross_root_files_falls_back_from_exdev(self) -> None:
        first = self.root1 / "first.txt"
        second = self.root1 / "second.txt"
        first.write_text("a", encoding="utf-8")
        second.write_text("b", encoding="utf-8")

        path_guard = PathGuard({"storage1": str(self.root1), "storage2": str(self.root2)})
        self._set_services(path_guard=path_guard, filesystem=CrossDeviceMoveFilesystemAdapter())

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/first.txt", "storage1/second.txt"],
                "destination_base": "storage2",
            },
        )

        self.assertEqual(response.status_code, 202)
        detail = self._wait_task(response.json()["task_id"])
        self.assertEqual(detail["status"], "completed")
        self.assertEqual(detail["done_items"], 2)
        self.assertEqual(detail["total_items"], 2)
        self.assertTrue((self.root2 / "first.txt").exists())
        self.assertTrue((self.root2 / "second.txt").exists())
        self.assertFalse(first.exists())
        self.assertFalse(second.exists())

    def test_move_batch_cross_root_directories_blocked(self) -> None:
        first = self.root1 / "first-dir"
        second = self.root1 / "second-dir"
        first.mkdir()
        second.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/first-dir", "storage1/second-dir"],
                "destination_base": "storage2",
            },
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()["error"]["code"], "invalid_request")
        self.assertEqual(response.json()["error"]["message"], "Cross-root batch move with directories is not supported in v1")

    def test_move_batch_cross_root_mixed_files_and_directories_blocked(self) -> None:
        first = self.root1 / "first.txt"
        first.write_text("a", encoding="utf-8")
        second = self.root1 / "second-dir"
        second.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/first.txt", "storage1/second-dir"],
                "destination_base": "storage2",
            },
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()["error"]["code"], "invalid_request")
        self.assertEqual(response.json()["error"]["message"], "Cross-root batch move with directories is not supported in v1")

    def test_move_batch_mixed_root_selection_blocked(self) -> None:
        first = self.root1 / "first-dir"
        second = self.root2 / "other-dir"
        first.mkdir()
        second.mkdir()
        target = self.root1 / "target"
        target.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/first-dir", "storage2/other-dir"],
                "destination_base": "storage1/target",
            },
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()["error"]["code"], "invalid_request")

    def test_move_batch_destination_exists_blocked(self) -> None:
        first = self.root1 / "first-dir"
        second = self.root1 / "second-dir"
        first.mkdir()
        second.mkdir()
        target = self.root1 / "target"
        target.mkdir()
        (target / "second-dir").mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/first-dir", "storage1/second-dir"],
                "destination_base": "storage1/target",
            },
        )

        self.assertEqual(response.status_code, 409)
        self.assertEqual(response.json()["error"]["code"], "already_exists")

    def test_move_batch_destination_inside_source_blocked(self) -> None:
        first = self.root1 / "first-dir"
        first.mkdir()
        (first / "child").mkdir()
        second = self.root1 / "second-dir"
        second.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/first-dir", "storage1/second-dir"],
                "destination_base": "storage1/first-dir/child",
            },
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()["error"]["code"], "invalid_request")

    def test_move_batch_symlink_source_blocked(self) -> None:
        real_dir = self.root1 / "real-dir"
        real_dir.mkdir()
        symlink = self.root1 / "dir-link"
        symlink.symlink_to(real_dir, target_is_directory=True)
        other = self.root1 / "other-dir"
        other.mkdir()
        target = self.root1 / "target"
        target.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/dir-link", "storage1/other-dir"],
                "destination_base": "storage1/target",
            },
        )

        self.assertEqual(response.status_code, 409)
        self.assertEqual(response.json()["error"]["code"], "type_conflict")

    def test_move_batch_runtime_io_error_failed_task_shape(self) -> None:
        first = self.root1 / "ok-dir"
        first.mkdir()
        (first / "a.txt").write_text("A", encoding="utf-8")
        second = self.root1 / "fail-dir"
        second.mkdir()
        (second / "b.txt").write_text("B", encoding="utf-8")
        target = self.root1 / "target"
        target.mkdir()

        path_guard = PathGuard({"storage1": str(self.root1), "storage2": str(self.root2)})
        self._set_services(path_guard=path_guard, filesystem=FailingBatchFilesystemAdapter())

        response = self._request(
            "POST",
            "/api/files/move",
            {
                "sources": ["storage1/ok-dir", "storage1/fail-dir"],
                "destination_base": "storage1/target",
            },
        )

        self.assertEqual(response.status_code, 202)
        detail = self._wait_task(response.json()["task_id"])
        self.assertEqual(detail["status"], "failed")
        self.assertEqual(detail["error_code"], "io_error")
        self.assertEqual(detail["done_items"], 1)
        self.assertEqual(detail["total_items"], 2)
        self.assertEqual(detail["failed_item"], str(second))
        self.assertTrue((target / "ok-dir").is_dir())
        self.assertTrue(second.exists())

    def test_move_source_not_found(self) -> None:
        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/missing.txt", "destination": "storage1/out.txt"},
        )

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

    def test_move_directory_source_not_found(self) -> None:
        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/missing-dir", "destination": "storage1/out-dir"},
        )

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

    def test_move_source_is_directory_type_conflict_for_file_destination_parent(self) -> None:
        (self.root1 / "dir").mkdir()
        (self.root1 / "out.txt").write_text("x", encoding="utf-8")

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/dir", "destination": "storage1/out.txt/child"},
        )

        self.assertEqual(response.status_code, 409)
        self.assertEqual(response.json()["error"]["code"], "type_conflict")

    def test_move_destination_exists_already_exists(self) -> None:
        (self.root1 / "source.txt").write_text("x", encoding="utf-8")
        (self.root1 / "exists.txt").write_text("y", encoding="utf-8")

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source.txt", "destination": "storage1/exists.txt"},
        )

        self.assertEqual(response.status_code, 409)
        self.assertEqual(response.json()["error"]["code"], "already_exists")

    def test_move_directory_destination_exists_already_exists(self) -> None:
        (self.root1 / "source-dir").mkdir()
        (self.root1 / "target-dir").mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source-dir", "destination": "storage1/target-dir"},
        )

        self.assertEqual(response.status_code, 409)
        self.assertEqual(response.json()["error"]["code"], "already_exists")

    def test_move_traversal_source(self) -> None:
        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/../etc/passwd", "destination": "storage1/out.txt"},
        )

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

    def test_move_traversal_destination(self) -> None:
        (self.root1 / "source.txt").write_text("x", encoding="utf-8")

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source.txt", "destination": "storage1/../etc/out.txt"},
        )

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

    def test_move_directory_destination_inside_source_blocked(self) -> None:
        src_dir = self.root1 / "source-dir"
        src_dir.mkdir()
        (src_dir / "child").mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source-dir", "destination": "storage1/source-dir/child/moved-dir"},
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()["error"]["code"], "invalid_request")

    def test_move_directory_same_source_destination_blocked(self) -> None:
        src_dir = self.root1 / "source-dir"
        src_dir.mkdir()

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source-dir", "destination": "storage1/source-dir"},
        )

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()["error"]["code"], "invalid_request")

    def test_move_source_symlink_rejected(self) -> None:
        target = self.root1 / "real.txt"
        target.write_text("x", encoding="utf-8")
        link = self.root1 / "link.txt"
        link.symlink_to(target)

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/link.txt", "destination": "storage1/out.txt"},
        )

        self.assertEqual(response.status_code, 409)
        self.assertEqual(response.json()["error"]["code"], "type_conflict")

    def test_move_directory_source_symlink_rejected(self) -> None:
        target = self.root1 / "real-dir"
        target.mkdir()
        (target / "nested.txt").write_text("x", encoding="utf-8")
        link = self.root1 / "dir-link"
        link.symlink_to(target, target_is_directory=True)

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/dir-link", "destination": "storage1/out-dir"},
        )

        self.assertEqual(response.status_code, 409)
        self.assertEqual(response.json()["error"]["code"], "type_conflict")

    def test_move_runtime_io_error_failed_task_shape(self) -> None:
        src = self.root1 / "source.txt"
        src.write_text("hello", encoding="utf-8")

        path_guard = PathGuard({"storage1": str(self.root1), "storage2": str(self.root2)})
        self._set_services(path_guard=path_guard, filesystem=FailingDeleteFilesystemAdapter())

        response = self._request(
            "POST",
            "/api/files/move",
            {"source": "storage1/source.txt", "destination": "storage2/moved.txt"},
        )
        self.assertEqual(response.status_code, 202)

        task_id = response.json()["task_id"]
        detail = self._wait_task(task_id)

        self.assertEqual(detail["status"], "failed")
        self.assertEqual(detail["error_code"], "io_error")
        self.assertTrue((self.root2 / "moved.txt").exists())
        self.assertTrue(src.exists())


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