# retoor <retoor@molodetz.nl>

import time
import pytest
import requests
from tests.conftest import BASE_URL
from devplacepy.database import init_db, get_table
from devplacepy import project_files as pf
from devplacepy.project_files import ProjectFileError
@pytest.fixture(autouse=True)
def _init_db_project_file_lines():
    init_db()
    yield
_pid = [0]
def _project():
    _pid[0] += 1
    pid = f"plines-{_pid[0]}"
    user = {"uid": f"plines-owner-{_pid[0]}"}
    return pid, user
_counter_project_file_lines = [0]
def _signup_project_file_lines():
    _counter_project_file_lines[0] += 1
    name = f"pl{int(time.time() * 1000)}{_counter_project_file_lines[0]}"
    requests.post(
        f"{BASE_URL}/auth/signup",
        data={
            "username": name,
            "email": f"{name}@t.dev",
            "password": "secret123",
            "confirm_password": "secret123",
            "birth_date": "1990-01-01",
            "accept_terms": "1",
        },
        allow_redirects=True,
    )
    return name, get_table("users").find_one(username=name)["api_key"]
def _h_project_file_lines(key):
    return {"X-API-KEY": key, "Accept": "application/json"}
def _create_project_project_file_lines(key, title):
    r = requests.post(
        f"{BASE_URL}/projects/create",
        headers=_h_project_file_lines(key),
        data={
            "title": title,
            "description": "lines test",
            "project_type": "software",
            "status": "In Development",
        },
    )
    assert r.status_code == 200, r.text
    return r.json()["data"]
def _write_project_file_lines(key, slug, path, content):
    r = requests.post(
        f"{BASE_URL}/projects/{slug}/files/write",
        headers=_h_project_file_lines(key),
        data={"path": path, "content": content},
        allow_redirects=False,
    )
    assert r.status_code in (200, 302), r.text
def _raw_project_file_lines(key, slug, path):
    return requests.get(
        f"{BASE_URL}/projects/{slug}/files/raw", headers=_h_project_file_lines(key), params={"path": path}
    ).json()
class _FakeResponse:
    def __init__(self, status_code=200):
        self.status_code = status_code
class _FakeClient:
    authenticated = True
    username = "u"

    def __init__(self):
        self.calls = []

    async def call(
        self, method, path, params=None, data=None, file_field=None, headers=None
    ):
        self.calls.append((method, path))
        return _FakeResponse()
def _make_dispatcher():
    import devplacepy.services.devii.actions.dispatcher as disp
    from devplacepy.services.devii.actions.catalog import PLATFORM_CATALOG

    d = disp.Dispatcher.__new__(disp.Dispatcher)
    d._actions = PLATFORM_CATALOG.by_name()
    d._client = _FakeClient()
    d._read_files = set()
    return disp, d
from playwright.sync_api import expect
from devplacepy.database import get_table
_counter_project_files = [0]
def _signup_project_files():
    _counter_project_files[0] += 1
    name = f"pf{int(time.time() * 1000)}{_counter_project_files[0]}"
    session = requests.Session()
    session.post(
        f"{BASE_URL}/auth/signup",
        data={
            "username": name,
            "email": f"{name}@t.dev",
            "password": "secret123",
            "confirm_password": "secret123",
            "birth_date": "1990-01-01",
            "accept_terms": "1",
        },
        allow_redirects=True,
    )
    key = get_table("users").find_one(username=name)["api_key"]
    return name, key
def _h_project_files(key):
    return {"X-API-KEY": key, "Accept": "application/json"}
def _create_project_project_files(key, title):
    r = requests.post(
        f"{BASE_URL}/projects/create",
        headers=_h_project_files(key),
        data={
            "title": title,
            "description": "filesystem test",
            "project_type": "software",
            "status": "In Development",
        },
    )
    assert r.status_code == 200, r.text
    return r.json()["data"]
def _write_project_files(key, slug, path, content):
    return requests.post(
        f"{BASE_URL}/projects/{slug}/files/write",
        headers=_h_project_files(key),
        data={"path": path, "content": content},
        allow_redirects=False,
    )
def _mkdir(key, slug, path):
    return requests.post(
        f"{BASE_URL}/projects/{slug}/files/mkdir",
        headers=_h_project_files(key),
        data={"path": path},
        allow_redirects=False,
    )
def _move(key, slug, from_path, to_path):
    return requests.post(
        f"{BASE_URL}/projects/{slug}/files/move",
        headers=_h_project_files(key),
        data={"from_path": from_path, "to_path": to_path},
        allow_redirects=False,
    )
def _delete(key, slug, path):
    return requests.post(
        f"{BASE_URL}/projects/{slug}/files/delete",
        headers=_h_project_files(key),
        data={"path": path},
        allow_redirects=False,
    )
def _list(slug, key=None):
    return requests.get(
        f"{BASE_URL}/projects/{slug}/files",
        headers=_h_project_files(key) if key else {"Accept": "application/json"},
    )
def _raw_project_files(slug, path, key=None):
    return requests.get(
        f"{BASE_URL}/projects/{slug}/files/raw",
        params={"path": path},
        headers=_h_project_files(key) if key else {"Accept": "application/json"},
    )
def _paths(slug, key=None):
    return sorted(f["path"] for f in _list(slug, key).json()["files"])
def _make_project_ui(page, title):
    page.goto(f"{BASE_URL}/projects", wait_until="domcontentloaded")
    page.locator("#create-project-btn").click()
    page.fill("#title", title)
    page.fill("#description", "Project for filesystem UI test")
    page.click("button:has-text('Create Project')")
    page.wait_for_url(f"{BASE_URL}/projects/*", wait_until="domcontentloaded")
    return page.url
def _open_files(page, title):
    proj_url = _make_project_ui(page, title)
    slug = proj_url.rstrip("/").split("/")[-1]
    page.goto(proj_url + "/files", wait_until="domcontentloaded")
    return slug
def _dialog_fill(page, value):
    page.locator(".dialog-overlay.visible .dialog-input").wait_for(state="visible")
    page.fill(".dialog-overlay.visible .dialog-input", value)
    page.click(".dialog-overlay.visible .dialog-confirm")
def _dialog_confirm(page):
    page.locator(".dialog-overlay.visible .dialog-confirm").wait_for(state="visible")
    page.click(".dialog-overlay.visible .dialog-confirm")
def _dialog_cancel(page):
    page.locator(".dialog-overlay.visible .dialog-cancel").wait_for(state="visible")
    page.click(".dialog-overlay.visible .dialog-cancel")
def _new_folder(page, name):
    page.click("#pf-new-folder")
    _dialog_fill(page, name)
    page.wait_for_selector(f".pf-node-row:has-text('{name.split('/')[-1]}')")
def _new_file(page, name):
    page.click("#pf-new-file")
    _dialog_fill(page, name)
def _row(page, name):
    return page.locator(f".pf-node-row:has-text('{name}')").first
def _alice_key():
    return get_table("users").find_one(username="alice_test")["api_key"]


def test_read_lines_range_and_total():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb\nc\nd")
    out = pf.read_lines(pid, "f.txt", 2, 3)
    assert out["lines"] == ["b", "c"]
    assert out["total_lines"] == 4
    assert out["content"] == "b\nc"


def test_read_lines_open_end_clamps():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb\nc")
    out = pf.read_lines(pid, "f.txt", 2, None)
    assert out["lines"] == ["b", "c"]
    assert out["end"] == 3


def test_replace_lines_middle():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb\nc\nd")
    pf.replace_lines(pid, "f.txt", 2, 3, "X\nY\nZ")
    assert pf.read_file(pid, "f.txt")["content"] == "a\nX\nY\nZ\nd"


def test_replace_lines_empty_content_deletes():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb\nc")
    pf.replace_lines(pid, "f.txt", 2, 2, "")
    assert pf.read_file(pid, "f.txt")["content"] == "a\nc"


def test_insert_lines_prepend_and_append_positions():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb")
    pf.insert_lines(pid, "f.txt", 1, "TOP")
    pf.insert_lines(pid, "f.txt", 99, "BOTTOM")
    assert pf.read_file(pid, "f.txt")["content"] == "TOP\na\nb\nBOTTOM"


def test_delete_lines_range():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb\nc\nd")
    pf.delete_lines(pid, "f.txt", 2, 3)
    assert pf.read_file(pid, "f.txt")["content"] == "a\nd"


def test_append_preserves_trailing_newline():
    pid, u = _project()
    pf.write_text_file(pid, u, "g.txt", "one\ntwo\n")
    pf.append_lines(pid, "g.txt", "three")
    assert pf.read_file(pid, "g.txt")["content"] == "one\ntwo\nthree\n"


def test_append_without_trailing_adds_newline_between():
    pid, u = _project()
    pf.write_text_file(pid, u, "g.txt", "one")
    pf.append_lines(pid, "g.txt", "two")
    assert pf.read_file(pid, "g.txt")["content"] == "one\ntwo"


def test_append_honors_content_trailing_newline():
    pid, u = _project()
    pf.write_text_file(pid, u, "g.txt", "a\nb")
    pf.append_lines(pid, "g.txt", "c\nd\n")
    assert pf.read_file(pid, "g.txt")["content"] == "a\nb\nc\nd\n"


def test_replace_last_line_honors_content_trailing_newline():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb\nc")
    pf.replace_lines(pid, "f.txt", 3, 3, "C\n")
    assert pf.read_file(pid, "f.txt")["content"] == "a\nb\nC\n"


def test_insert_at_end_honors_content_trailing_newline():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb")
    pf.insert_lines(pid, "f.txt", 3, "c\n")
    assert pf.read_file(pid, "f.txt")["content"] == "a\nb\nc\n"


def test_interior_replace_does_not_add_trailing_newline():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb\nc\nd")
    pf.replace_lines(pid, "f.txt", 2, 2, "X\n")
    assert pf.read_file(pid, "f.txt")["content"] == "a\nX\nc\nd"


def test_line_ops_reject_missing_dir_and_binary():
    pid, u = _project()
    pf.make_dir(pid, u, "adir")
    pf.store_upload(pid, u, "", "blob.bin", bytes(range(64)))
    with pytest.raises(ProjectFileError):
        pf.read_lines(pid, "nope.txt")
    with pytest.raises(ProjectFileError):
        pf.replace_lines(pid, "adir", 1, 1, "x")
    with pytest.raises(ProjectFileError):
        pf.append_lines(pid, "blob.bin", "x")


def test_replace_out_of_range_raises():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "a\nb")
    with pytest.raises(ProjectFileError):
        pf.replace_lines(pid, "f.txt", 9, 9, "x")


def test_append_respects_max_chars():
    pid, u = _project()
    pf.write_text_file(pid, u, "f.txt", "x")
    with pytest.raises(ProjectFileError):
        pf.append_lines(pid, "f.txt", "y" * (pf.MAX_TEXT_CHARS + 10))


def test_normalize_blocks_traversal_in_line_ops():
    pid, u = _project()
    with pytest.raises(ProjectFileError):
        pf.read_lines(pid, "../escape.txt")


def test_write_blocked_until_read(monkeypatch):
    from tests.conftest import run_async

    disp, d = _make_dispatcher()
    monkeypatch.setattr(disp, "format_response", lambda r: "ok")
    monkeypatch.setattr(disp, "record_mutation", lambda name: None)
    monkeypatch.setattr(disp, "get_store", lambda: None)
    actions = d._actions
    write = actions["project_write_file"]
    read = actions["project_read_file"]
    args = {"project_slug": "p", "path": "src/app.py", "content": "x"}

    from devplacepy.services.devii.errors import ToolInputError

    with pytest.raises(ToolInputError):
        run_async(d._run_http(write, args))
    assert d._client.calls == [
        ("GET", "/projects/p/files/raw")
    ], "write blocked before read should only trigger a file-exists check"

    run_async(d._run_http(read, {"project_slug": "p", "path": "src/app.py"}))
    run_async(d._run_http(write, args))
    assert ("POST", "/projects/p/files/write") in d._client.calls


def test_guard_path_normalization_matches(monkeypatch):
    from tests.conftest import run_async

    disp, d = _make_dispatcher()
    monkeypatch.setattr(disp, "format_response", lambda r: "ok")
    monkeypatch.setattr(disp, "record_mutation", lambda name: None)
    monkeypatch.setattr(disp, "get_store", lambda: None)
    actions = d._actions
    # read with a messy path, write with the clean path - normalization should unify them
    run_async(
        d._run_http(
            actions["project_read_file"], {"project_slug": "p", "path": "/src//app.py"}
        )
    )
    run_async(
        d._run_http(
            actions["project_write_file"],
            {"project_slug": "p", "path": "src/app.py", "content": "x"},
        )
    )
    assert ("POST", "/projects/p/files/write") in d._client.calls


def test_devii_actions_registered():
    from devplacepy.services.devii.actions.catalog import ACTIONS

    names = {a.name for a in ACTIONS}
    assert {
        "project_list_files",
        "project_read_file",
        "project_write_file",
        "project_upload_file",
        "project_make_dir",
        "project_move_file",
        "project_delete_file",
    } <= names


def test_docs_group_present():
    from devplacepy import docs_api

    group = next((g for g in docs_api.API_GROUPS if g["slug"] == "project-files"), None)
    assert group is not None
    ids = {e["id"] for e in group["endpoints"]}
    assert "project-files-write" in ids and "project-files-list" in ids


def _insert_binary_node(project_uid, directory, stored_name, deleted_at=None):
    from devplacepy.utils import generate_uid

    uid = generate_uid()
    pf._table().insert(
        {
            "uid": uid,
            "project_uid": project_uid,
            "deleted_at": deleted_at,
            "deleted_by": None,
            "user_uid": "system-prune-test-user",
            "path": f"/{stored_name}",
            "name": stored_name,
            "parent_path": "/",
            "type": "file",
            "content": None,
            "is_binary": 1,
            "stored_name": stored_name,
            "directory": directory,
            "mime_type": "application/octet-stream",
            "size": 0,
            "created_at": pf._now(),
            "updated_at": pf._now(),
        }
    )
    return uid


def _write_project_blob(base, directory, stored_name, content=b"data"):
    file_dir = base / directory
    file_dir.mkdir(parents=True, exist_ok=True)
    (file_dir / stored_name).write_bytes(content)


def test_purge_soft_deleted_project_files_removes_row_and_blob(local_db, tmp_path, monkeypatch):
    monkeypatch.setattr(pf, "PROJECT_FILES_DIR", tmp_path)
    directory = "ab/cd"
    stored_name = "obj.o"
    _write_project_blob(tmp_path, directory, stored_name, b"x" * 40)
    uid = _insert_binary_node(
        "sys-prune-proj-1", directory, stored_name, deleted_at="2020-01-01T00:00:00+00:00"
    )

    removed, freed = pf.purge_soft_deleted_project_files()

    assert removed >= 1
    assert freed == 40
    assert pf._table().find_one(uid=uid) is None
    assert not (tmp_path / directory / stored_name).exists()


def test_purge_soft_deleted_project_files_dry_run_changes_nothing(local_db, tmp_path, monkeypatch):
    monkeypatch.setattr(pf, "PROJECT_FILES_DIR", tmp_path)
    directory = "ab/cd"
    stored_name = "obj2.o"
    _write_project_blob(tmp_path, directory, stored_name, b"x" * 15)
    uid = _insert_binary_node(
        "sys-prune-proj-2", directory, stored_name, deleted_at="2020-01-01T00:00:00+00:00"
    )

    removed, freed = pf.purge_soft_deleted_project_files(dry_run=True)

    assert removed >= 1
    assert freed == 15
    assert pf._table().find_one(uid=uid) is not None
    assert (tmp_path / directory / stored_name).exists()


def test_purge_soft_deleted_project_files_ignores_live_and_text_rows(
    local_db, tmp_path, monkeypatch
):
    monkeypatch.setattr(pf, "PROJECT_FILES_DIR", tmp_path)
    directory = "ab/cd"
    stored_name = "live.o"
    _write_project_blob(tmp_path, directory, stored_name)
    live_uid = _insert_binary_node("sys-prune-proj-3", directory, stored_name, deleted_at=None)

    pf.purge_soft_deleted_project_files()

    assert pf._table().find_one(uid=live_uid) is not None
    assert (tmp_path / directory / stored_name).exists()


def test_sweep_orphan_project_file_blobs_removes_unreferenced_file(local_db, tmp_path, monkeypatch):
    monkeypatch.setattr(pf, "PROJECT_FILES_DIR", tmp_path)
    directory = "ab/cd"
    orphan_name = "leftover.right"
    _write_project_blob(tmp_path, directory, orphan_name, b"q" * 60)

    removed, freed = pf.sweep_orphan_project_file_blobs()

    assert removed == 1
    assert freed == 60
    assert not (tmp_path / directory / orphan_name).exists()


def test_sweep_orphan_project_file_blobs_keeps_referenced_file(local_db, tmp_path, monkeypatch):
    monkeypatch.setattr(pf, "PROJECT_FILES_DIR", tmp_path)
    directory = "ab/cd"
    stored_name = "kept.bin"
    _write_project_blob(tmp_path, directory, stored_name)
    _insert_binary_node("sys-prune-proj-4", directory, stored_name, deleted_at=None)

    removed, freed = pf.sweep_orphan_project_file_blobs()

    assert removed == 0
    assert freed == 0
    assert (tmp_path / directory / stored_name).exists()


def test_sweep_orphan_project_file_blobs_keeps_blob_referenced_only_by_soft_deleted_row(
    local_db, tmp_path, monkeypatch
):
    monkeypatch.setattr(pf, "PROJECT_FILES_DIR", tmp_path)
    directory = "ab/cd"
    stored_name = "still-soft-deleted.bin"
    _write_project_blob(tmp_path, directory, stored_name)
    _insert_binary_node(
        "sys-prune-proj-5", directory, stored_name, deleted_at="2020-01-01T00:00:00+00:00"
    )

    removed, freed = pf.sweep_orphan_project_file_blobs()

    assert removed == 0
    assert freed == 0
    assert (tmp_path / directory / stored_name).exists()


def test_sweep_orphan_project_file_blobs_dry_run_changes_nothing(local_db, tmp_path, monkeypatch):
    monkeypatch.setattr(pf, "PROJECT_FILES_DIR", tmp_path)
    directory = "ab/cd"
    orphan_name = "dry.right"
    _write_project_blob(tmp_path, directory, orphan_name, b"w" * 12)

    removed, freed = pf.sweep_orphan_project_file_blobs(dry_run=True)

    assert removed == 1
    assert freed == 12
    assert (tmp_path / directory / orphan_name).exists()
