# 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"]
import asyncio
import io
import json
import zipfile
from pathlib import Path
from devplacepy.database import get_table, refresh_snapshot
from devplacepy.services.jobs import queue
from devplacepy.services.jobs.zip_service import ZipService
from tests.conftest import run_async
_counter_zip_download = [0]
def _signup_zip_download():
    _counter_zip_download[0] += 1
    name = f"zd{int(time.time() * 1000)}{_counter_zip_download[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,
    )
    refresh_snapshot()
    key = get_table("users").find_one(username=name)["api_key"]
    return name, key
def _h_zip_download(key):
    return {"X-API-KEY": key, "Accept": "application/json"}
def _create_project_zip_download(key, title):
    r = requests.post(
        f"{BASE_URL}/projects/create",
        headers=_h_zip_download(key),
        data={
            "title": title,
            "description": "zip download test",
            "project_type": "software",
            "status": "In Development",
        },
    )
    assert r.status_code == 200, r.text
    return r.json()["data"]
def _write_zip_download(key, slug, path, content):
    r = requests.post(
        f"{BASE_URL}/projects/{slug}/files/write",
        headers=_h_zip_download(key),
        data={"path": path, "content": content},
        allow_redirects=False,
    )
    assert r.status_code in (200, 302), r.text
def _process_uid(uid):
    async def drive():
        svc = ZipService()
        for _ in range(400):
            await svc.run_once()
            refresh_snapshot()
            job = queue.get_job(uid)
            if job and job["status"] in ("done", "failed"):
                return
            await asyncio.sleep(0.05)

    run_async(drive())
def _drain_pending():
    async def drive():
        svc = ZipService()
        appeared = False
        for _ in range(600):
            refresh_snapshot()
            pending = [
                r
                for r in get_table("jobs").find(kind="zip")
                if r["status"] in ("pending", "running")
            ]
            if pending:
                appeared = True
            await svc.run_once()
            refresh_snapshot()
            pending = [
                r
                for r in get_table("jobs").find(kind="zip")
                if r["status"] in ("pending", "running")
            ]
            if appeared and not pending and not svc._inflight:
                return
            await asyncio.sleep(0.05)

    run_async(drive())
def _cleanup_archives():
    refresh_snapshot()
    jobs = get_table("jobs")
    for row in list(jobs.find(kind="zip")):
        result = json.loads(row.get("result") or "{}")
        local_path = result.get("local_path")
        if local_path:
            Path(local_path).unlink(missing_ok=True)
        jobs.delete(uid=row["uid"])
def _login_zip_download(page, name):
    page.goto(f"{BASE_URL}/auth/login", wait_until="domcontentloaded")
    page.fill("#email", f"{name}@t.dev")
    page.fill("#password", "secret123")
    page.click("button:has-text('Sign in')")
    page.wait_for_url("**/feed", timeout=10000, wait_until="domcontentloaded")


def test_long_press_opens_context_menu(mobile_page):
    page, _ = mobile_page
    key = get_table("users").find_one(username="alice_test")["api_key"]
    project = _create_project_project_files(key, "Mobile LongPress")
    slug = project["slug"]
    _write_project_files(key, slug, "touch.py", "x = 1")
    page.goto(f"{BASE_URL}/projects/{slug}/files", wait_until="domcontentloaded")
    page.wait_for_selector(".pf-node-row:has-text('touch.py')")
    page.eval_on_selector(
        ".pf-node-row:has-text('touch.py')",
        """el => {
        const r = el.getBoundingClientRect();
        const t = new Touch({identifier: 1, target: el, clientX: r.left + 10, clientY: r.top + 10});
        el.dispatchEvent(new TouchEvent('touchstart', {bubbles: true, cancelable: true, touches: [t], targetTouches: [t], changedTouches: [t]}));
    }""",
    )
    page.wait_for_timeout(700)
    expect(page.locator(".context-menu.visible")).to_be_visible()
    expect(page.locator(".context-menu.visible")).to_contain_text("Delete")


def test_delete_open_file_opens_next(alice):
    page, _ = alice
    key = _alice_key()
    project = _create_project_project_files(key, "DeleteOpensNext")
    slug = project["slug"]
    _write_project_files(key, slug, "a.py", "1")
    _write_project_files(key, slug, "b.py", "2")
    page.goto(f"{BASE_URL}/projects/{slug}/files", wait_until="domcontentloaded")
    expect(page.locator("#pf-editor-wrap")).to_be_visible()
    _row(page, "a.py").click()
    page.click("#pf-delete")
    _dialog_confirm(page)
    page.wait_for_timeout(400)
    assert _raw_project_files(slug, "a.py").status_code == 404
    expect(page.locator("#pf-editor-wrap")).to_be_visible()
    expect(page.locator("#pf-empty")).to_be_hidden()
    _row(page, "b.py").click()
    page.click("#pf-delete")
    _dialog_confirm(page)
    page.wait_for_timeout(400)
    expect(page.locator("#pf-empty")).to_be_visible()


def test_files_toolbar_has_download_button(app_server, page):
    name, key = _signup_zip_download()
    proj = _create_project_zip_download(key, "Files Toolbar UI")
    slug = proj["slug"] or proj["uid"]
    _write_zip_download(key, slug, "README.md", "# hi")
    _login_zip_download(page, name)
    page.goto(f"{BASE_URL}/projects/{slug}/files", wait_until="domcontentloaded")
    expect(page.locator("button:has-text('Download as zip')")).to_be_visible()


def test_files_context_menu_has_download(app_server, page):
    name, key = _signup_zip_download()
    proj = _create_project_zip_download(key, "Files Context UI")
    slug = proj["slug"] or proj["uid"]
    _write_zip_download(key, slug, "README.md", "# hi")
    _login_zip_download(page, name)
    page.goto(f"{BASE_URL}/projects/{slug}/files", wait_until="domcontentloaded")
    row = page.locator(".pf-node-row").first
    row.wait_for(state="visible")
    row.click(button="right")
    expect(
        page.locator(".context-menu-item:has-text('Download as zip')").first
    ).to_be_visible()
