from __future__ import annotations

import asyncio
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_copy_task_service, get_task_service
from backend.app.db.task_repository import TaskRepository
from backend.app.main import app
from backend.app.security.path_guard import PathGuard
from backend.app.services.copy_task_service import CopyTaskService
from backend.app.services.task_service import TaskService
from backend.app.tasks_runner import TaskRunner
from backend.app.fs.filesystem_adapter import FilesystemAdapter


class FailingFilesystemAdapter(FilesystemAdapter):
    def copy_file(self, source: str, destination: str, on_progress: callable | None = None) -> None:
        raise OSError("forced copy failure")

    def copy_directory(self, source: str, destination: str) -> None:
        raise OSError("forced copy failure")


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

    def copy_file(self, source: str, destination: str, on_progress: callable | None = None) -> None:
        self.entered.set()
        self.release.wait(timeout=2.0)
        return super().copy_file(source=source, destination=destination, on_progress=on_progress)


class CopyApiGoldenTest(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.repo = TaskRepository(str(Path(self.temp_dir.name) / "tasks.db"))

        path_guard = PathGuard({"storage1": str(self.root), "storage2": str(self.root)})
        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)
        copy_service = CopyTaskService(path_guard=path_guard, repository=self.repo, runner=runner)
        task_service = TaskService(repository=self.repo)

        async def _override_copy_service() -> CopyTaskService:
            return copy_service

        async def _override_task_service() -> TaskService:
            return task_service

        app.dependency_overrides[get_copy_task_service] = _override_copy_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_copy_success_create_task_shape(self) -> None:
        src = self.root / "source.txt"
        src.write_text("hello", encoding="utf-8")

        response = self._request(
            "POST",
            "/api/files/copy",
            {"source": "storage1/source.txt", "destination": "storage1/copy.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.assertEqual(detail["total_bytes"], 5)
        self.assertEqual(detail["done_bytes"], 5)
        self.assertTrue((self.root / "copy.txt").exists())
        self.assertEqual((self.root / "copy.txt").read_text(encoding="utf-8"), "hello")

    def test_copy_batch_multi_file_success(self) -> None:
        (self.root / "a.txt").write_text("A", encoding="utf-8")
        (self.root / "b.txt").write_text("B", encoding="utf-8")
        (self.root / "dest").mkdir()

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

        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.assertEqual((self.root / "dest" / "a.txt").read_text(encoding="utf-8"), "A")
        self.assertEqual((self.root / "dest" / "b.txt").read_text(encoding="utf-8"), "B")

    def test_copy_single_directory_success(self) -> None:
        src = self.root / "photos"
        (src / "nested").mkdir(parents=True)
        (src / "cover.jpg").write_text("img", encoding="utf-8")
        (src / "nested" / "a.txt").write_text("nested", encoding="utf-8")

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

        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.root / "photos-copy").is_dir())
        self.assertEqual((self.root / "photos-copy" / "cover.jpg").read_text(encoding="utf-8"), "img")
        self.assertEqual((self.root / "photos-copy" / "nested" / "a.txt").read_text(encoding="utf-8"), "nested")

    def test_copy_batch_multi_directory_success(self) -> None:
        (self.root / "dir1" / "sub").mkdir(parents=True)
        (self.root / "dir2").mkdir()
        (self.root / "dir1" / "sub" / "a.txt").write_text("A", encoding="utf-8")
        (self.root / "dir2" / "b.txt").write_text("B", encoding="utf-8")
        (self.root / "dest").mkdir()

        response = self._request(
            "POST",
            "/api/files/copy",
            {
                "sources": ["storage1/dir1", "storage1/dir2"],
                "destination_base": "storage1/dest",
            },
        )

        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.assertEqual((self.root / "dest" / "dir1" / "sub" / "a.txt").read_text(encoding="utf-8"), "A")
        self.assertEqual((self.root / "dest" / "dir2" / "b.txt").read_text(encoding="utf-8"), "B")

    def test_copy_batch_mixed_file_and_directory_success(self) -> None:
        (self.root / "file.txt").write_text("F", encoding="utf-8")
        (self.root / "docs" / "nested").mkdir(parents=True)
        (self.root / "docs" / "nested" / "note.txt").write_text("N", encoding="utf-8")
        (self.root / "dest").mkdir()

        response = self._request(
            "POST",
            "/api/files/copy",
            {
                "sources": ["storage1/file.txt", "storage1/docs"],
                "destination_base": "storage1/dest",
            },
        )

        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.assertEqual((self.root / "dest" / "file.txt").read_text(encoding="utf-8"), "F")
        self.assertEqual((self.root / "dest" / "docs" / "nested" / "note.txt").read_text(encoding="utf-8"), "N")

    def test_copy_batch_cancelled_after_current_file_finishes(self) -> None:
        blocking_fs = BlockingCopyFilesystemAdapter()
        path_guard = PathGuard({"storage1": str(self.root), "storage2": str(self.root)})
        self._set_services(path_guard=path_guard, filesystem=blocking_fs)
        (self.root / "a.txt").write_text("A", encoding="utf-8")
        (self.root / "b.txt").write_text("B", encoding="utf-8")
        (self.root / "dest").mkdir()

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

        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((self.root / "dest" / "a.txt").exists())
        self.assertFalse((self.root / "dest" / "b.txt").exists())

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

        self.assertEqual(response.status_code, 404)
        self.assertEqual(
            response.json(),
            {
                "error": {
                    "code": "path_not_found",
                    "message": "Requested path was not found",
                    "details": {"path": "storage1/missing.txt"},
                }
            },
        )

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

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

        self.assertEqual(response.status_code, 409)
        self.assertEqual(
            response.json(),
            {
                "error": {
                    "code": "already_exists",
                    "message": "Target path already exists",
                    "details": {"path": "storage1/exists.txt"},
                }
            },
        )

    def test_copy_directory_destination_exists_already_exists(self) -> None:
        (self.root / "src").mkdir()
        (self.root / "src" / "a.txt").write_text("x", encoding="utf-8")
        (self.root / "exists").mkdir()

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

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

    def test_copy_batch_destination_exists_already_exists(self) -> None:
        (self.root / "a.txt").write_text("A", encoding="utf-8")
        (self.root / "dest").mkdir()
        (self.root / "dest" / "a.txt").write_text("exists", encoding="utf-8")
        (self.root / "b.txt").write_text("B", encoding="utf-8")

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

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

    def test_copy_traversal_source(self) -> None:
        response = self._request(
            "POST",
            "/api/files/copy",
            {"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_copy_traversal_destination(self) -> None:
        (self.root / "source.txt").write_text("x", encoding="utf-8")

        response = self._request(
            "POST",
            "/api/files/copy",
            {"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_copy_invalid_root_alias(self) -> None:
        (self.root / "source.txt").write_text("x", encoding="utf-8")

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

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

    def test_copy_destination_inside_directory_source_blocked(self) -> None:
        (self.root / "src").mkdir()
        (self.root / "src" / "a.txt").write_text("x", encoding="utf-8")

        response = self._request(
            "POST",
            "/api/files/copy",
            {"source": "storage1/src", "destination": "storage1/src/child"},
        )

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

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

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

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

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

        path_guard = PathGuard({"storage1": str(self.root), "storage2": str(self.root)})
        self._set_services(path_guard=path_guard, filesystem=FailingFilesystemAdapter())

        response = self._request(
            "POST",
            "/api/files/copy",
            {"source": "storage1/source.txt", "destination": "storage1/copy.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.assertEqual(detail["failed_item"], str(src))


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