# 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 pytest
from devplacepy.utils import clear_user_cache
from devplacepy import project_files
from devplacepy.project_files import ProjectFileError
from devplacepy.services.devii.actions.dispatcher import (
    confirmation_error,
    _is_confirmed,
)
_counter_project_visibility = [0]
def _signup_project_visibility():
    _counter_project_visibility[0] += 1
    name = f"pv{int(time.time() * 1000)}{_counter_project_visibility[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,
    )
    row = get_table("users").find_one(username=name)
    return name, row["uid"], row["api_key"]
def _make_admin_project_visibility():
    name, uid, key = _signup_project_visibility()
    get_table("users").update({"uid": uid, "role": "Admin"}, ["uid"])
    clear_user_cache(uid)
    return name, uid, key
def _h_project_visibility(key=None):
    headers = {"Accept": "application/json"}
    if key:
        headers["X-API-KEY"] = key
    return headers
def _create_project_project_visibility(key, title, is_private=False):
    data = {
        "title": title,
        "description": "visibility test",
        "project_type": "software",
        "status": "In Development",
    }
    if is_private:
        data["is_private"] = "on"
    r = requests.post(f"{BASE_URL}/projects/create", headers=_h_project_visibility(key), data=data)
    assert r.status_code == 200, r.text
    return r.json()["data"]
def _project_uid(slug):
    return get_table("projects").find_one(slug=slug)["uid"]
def _write_project_visibility(key, slug, path, content):
    return requests.post(
        f"{BASE_URL}/projects/{slug}/files/write",
        headers=_h_project_visibility(key),
        data={"path": path, "content": content},
        allow_redirects=False,
    )
def _set_private(key, slug, value):
    return requests.post(
        f"{BASE_URL}/projects/{slug}/private",
        headers=_h_project_visibility(key),
        data={"value": 1 if value else 0},
        allow_redirects=False,
    )
def _set_readonly(key, slug, value):
    return requests.post(
        f"{BASE_URL}/projects/{slug}/readonly",
        headers=_h_project_visibility(key),
        data={"value": 1 if value else 0},
        allow_redirects=False,
    )
def _list_slugs(key=None, user_uid=None):
    params = {"user_uid": user_uid} if user_uid else None
    r = requests.get(f"{BASE_URL}/projects", headers=_h_project_visibility(key), params=params)
    return [p["slug"] for p in r.json()["projects"]]


def test_project_delete_purges_files(app_server):
    _, key = _signup_project_files()
    project = _create_project_project_files(key, "FS Cascade")
    slug, uid = project["slug"], project["uid"]
    _write_project_files(key, slug, "src/a.py", "1")
    _write_project_files(key, slug, "src/b.py", "2")
    assert get_table("project_files").count(project_uid=uid) == 3
    r = requests.post(
        f"{BASE_URL}/projects/delete/{slug}", headers=_h_project_files(key), allow_redirects=False
    )
    assert r.status_code in (200, 302)
    from devplacepy.database import refresh_snapshot

    refresh_snapshot()
    assert get_table("project_files").count(project_uid=uid, deleted_at=None) == 0


def test_delete_project_works_when_readonly(app_server):
    _, _, key = _signup_project_visibility()
    project = _create_project_project_visibility(key, "Readonly Delete")
    slug = project["slug"]
    _write_project_visibility(key, slug, "a.txt", "one")
    _set_readonly(key, slug, True)
    r = requests.post(
        f"{BASE_URL}/projects/delete/{slug}", headers=_h_project_visibility(key), allow_redirects=False
    )
    assert r.status_code == 200 and r.json()["ok"] is True
    assert (
        requests.get(f"{BASE_URL}/projects/{slug}", headers=_h_project_visibility(key)).status_code == 404
    )
