# 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 devplacepy.database import get_table
from devplacepy.utils import clear_user_cache
from devplacepy import project_files
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_http_replace_insert_delete_append_roundtrip(app_server):
    _, key = _signup_project_file_lines()
    proj = _create_project_project_file_lines(key, "HTTP Edit Roundtrip")
    slug = proj["slug"] or proj["uid"]
    _write_project_file_lines(key, slug, "f.txt", "a\nb\nc\nd")
    requests.post(
        f"{BASE_URL}/projects/{slug}/files/replace-lines",
        headers=_h_project_file_lines(key),
        data={"path": "f.txt", "start": 2, "end": 3, "content": "X\nY"},
        allow_redirects=False,
    )
    assert _raw_project_file_lines(key, slug, "f.txt")["content"] == "a\nX\nY\nd"
    requests.post(
        f"{BASE_URL}/projects/{slug}/files/insert-lines",
        headers=_h_project_file_lines(key),
        data={"path": "f.txt", "at": 1, "content": "TOP"},
        allow_redirects=False,
    )
    assert _raw_project_file_lines(key, slug, "f.txt")["content"] == "TOP\na\nX\nY\nd"
    requests.post(
        f"{BASE_URL}/projects/{slug}/files/delete-lines",
        headers=_h_project_file_lines(key),
        data={"path": "f.txt", "start": 1, "end": 1},
        allow_redirects=False,
    )
    assert _raw_project_file_lines(key, slug, "f.txt")["content"] == "a\nX\nY\nd"
    requests.post(
        f"{BASE_URL}/projects/{slug}/files/append",
        headers=_h_project_file_lines(key),
        data={"path": "f.txt", "content": "END"},
        allow_redirects=False,
    )
    assert _raw_project_file_lines(key, slug, "f.txt")["content"] == "a\nX\nY\nd\nEND"


def test_http_replace_lines_non_owner_denied(app_server):
    _, owner_key = _signup_project_file_lines()
    _, other_key = _signup_project_file_lines()
    proj = _create_project_project_file_lines(owner_key, "HTTP Owner Guard")
    slug = proj["slug"] or proj["uid"]
    _write_project_file_lines(owner_key, slug, "f.txt", "a\nb")
    r = requests.post(
        f"{BASE_URL}/projects/{slug}/files/replace-lines",
        headers=_h_project_file_lines(other_key),
        data={"path": "f.txt", "start": 1, "end": 1, "content": "x"},
        allow_redirects=False,
    )
    assert r.status_code == 403


def test_readonly_blocks_every_mutation(app_server):
    _, _, key = _signup_project_visibility()
    slug = _create_project_project_visibility(key, "Readonly Block")["slug"]
    _write_project_visibility(key, slug, "main.py", "print(1)\n")
    assert _set_readonly(key, slug, True).status_code == 200

    assert _write_project_visibility(key, slug, "main.py", "print(2)\n").status_code == 400
    assert _write_project_visibility(key, slug, "new.py", "print(3)\n").status_code == 400
    assert (
        requests.post(
            f"{BASE_URL}/projects/{slug}/files/mkdir",
            headers=_h_project_visibility(key),
            data={"path": "docs"},
            allow_redirects=False,
        ).status_code
        == 400
    )
    assert (
        requests.post(
            f"{BASE_URL}/projects/{slug}/files/append",
            headers=_h_project_visibility(key),
            data={"path": "main.py", "content": "x"},
            allow_redirects=False,
        ).status_code
        == 400
    )
    assert (
        requests.post(
            f"{BASE_URL}/projects/{slug}/files/replace-lines",
            headers=_h_project_visibility(key),
            data={"path": "main.py", "start": 1, "end": 1, "content": "z"},
            allow_redirects=False,
        ).status_code
        == 400
    )
    assert (
        requests.post(
            f"{BASE_URL}/projects/{slug}/files/move",
            headers=_h_project_visibility(key),
            data={"from_path": "main.py", "to_path": "renamed.py"},
            allow_redirects=False,
        ).status_code
        == 400
    )
    assert (
        requests.post(
            f"{BASE_URL}/projects/{slug}/files/delete",
            headers=_h_project_visibility(key),
            data={"path": "main.py"},
            allow_redirects=False,
        ).status_code
        == 400
    )
    assert (
        requests.post(
            f"{BASE_URL}/projects/{slug}/files/upload",
            headers=_h_project_visibility(key),
            files={"file": ("u.py", b"x=1\n")},
            data={"path": ""},
        ).status_code
        == 400
    )
