# retoor <retoor@molodetz.nl>

import time
import requests
from playwright.sync_api import expect
from tests.conftest import BASE_URL
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_write_creates_file_and_parents(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS Write")["slug"]
    r = _write_project_files(key, slug, "src/app/main.py", "print('hi')\n")
    assert r.status_code == 200 and r.json()["ok"] is True
    assert _paths(slug, key) == ["src", "src/app", "src/app/main.py"]


def test_read_returns_content(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS Read")["slug"]
    _write_project_files(key, slug, "notes.txt", "hello world")
    r = _raw_project_files(slug, "notes.txt", key)
    body = r.json()
    assert r.status_code == 200
    assert body["content"] == "hello world"
    assert body["is_binary"] is False and body["type"] == "file"


def test_write_overwrites_existing(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS Overwrite")["slug"]
    _write_project_files(key, slug, "a.py", "one")
    _write_project_files(key, slug, "a.py", "two")
    assert _raw_project_files(slug, "a.py", key).json()["content"] == "two"
    assert _paths(slug, key) == ["a.py"]


def test_list_empty_project(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS Empty")["slug"]
    r = _list(slug, key)
    assert (
        r.status_code == 200
        and r.json()["files"] == []
        and r.json()["is_owner"] is True
    )


def test_mkdir_recursive(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS Mkdir")["slug"]
    r = _mkdir(key, slug, "a/b/c")
    assert r.status_code == 200
    files = {f["path"]: f["type"] for f in _list(slug, key).json()["files"]}
    assert files == {"a": "dir", "a/b": "dir", "a/b/c": "dir"}


def test_mkdir_idempotent(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS MkdirTwice")["slug"]
    assert _mkdir(key, slug, "docs").status_code == 200
    assert _mkdir(key, slug, "docs").status_code == 200
    assert _paths(slug, key) == ["docs"]


def test_move_file(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS MoveFile")["slug"]
    _write_project_files(key, slug, "old.py", "data")
    assert _move(key, slug, "old.py", "src/new.py").status_code == 200
    assert _paths(slug, key) == ["src", "src/new.py"]
    assert _raw_project_files(slug, "src/new.py", key).json()["content"] == "data"


def test_move_directory_with_descendants(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS MoveDir")["slug"]
    _write_project_files(key, slug, "src/a.py", "1")
    _write_project_files(key, slug, "src/sub/b.py", "2")
    assert _move(key, slug, "src", "lib").status_code == 200
    assert _paths(slug, key) == ["lib", "lib/a.py", "lib/sub", "lib/sub/b.py"]


def test_delete_file(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS DeleteFile")["slug"]
    _write_project_files(key, slug, "x.py", "1")
    assert _delete(key, slug, "x.py").status_code == 200
    assert _paths(slug, key) == []


def test_delete_directory_recursive(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS DeleteDir")["slug"]
    _write_project_files(key, slug, "src/a.py", "1")
    _write_project_files(key, slug, "src/sub/b.py", "2")
    _write_project_files(key, slug, "keep.py", "3")
    assert _delete(key, slug, "src").status_code == 200
    assert _paths(slug, key) == ["keep.py"]


def test_write_rejects_traversal(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS Traversal")["slug"]
    r = _write_project_files(key, slug, "../../etc/passwd", "x")
    assert r.status_code == 400
    assert _paths(slug, key) == []


def test_mkdir_rejects_dotdot(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS Dotdot")["slug"]
    assert _mkdir(key, slug, "a/../../b").status_code == 400


def test_raw_missing_file_404(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS Missing")["slug"]
    assert _raw_project_files(slug, "nope.py", key).status_code == 404


def test_public_read_without_auth(app_server):
    _, key = _signup_project_files()
    slug = _create_project_project_files(key, "FS Public")["slug"]
    _write_project_files(key, slug, "README.md", "# hi")
    r = _list(slug)
    assert r.status_code == 200 and r.json()["is_owner"] is False
    assert _raw_project_files(slug, "README.md").json()["content"] == "# hi"


def test_non_owner_cannot_write(app_server):
    _, owner_key = _signup_project_files()
    _, other_key = _signup_project_files()
    slug = _create_project_project_files(owner_key, "FS Perms")["slug"]
    r = _write_project_files(other_key, slug, "evil.py", "x")
    assert r.status_code == 403
    assert _paths(slug, owner_key) == []


def test_unknown_project_404(app_server):
    assert _list("does-not-exist").status_code == 404
