# retoor <retoor@molodetz.nl>

import requests

from devplacepy.services.game import store
from tests.api.game.index import JSON, _reset_farm, _signup
from tests.conftest import BASE_URL


def test_view_farm_public_json(app_server, seeded_db):
    _reset_farm("alice_test")
    response = requests.get(f"{BASE_URL}/game/farm/alice_test", headers=JSON)
    assert response.status_code == 200
    assert response.json()["farm"]["owner_username"] == "alice_test"


def test_view_unknown_farm_returns_404(app_server, seeded_db):
    response = requests.get(f"{BASE_URL}/game/farm/nobody_xyz", headers=JSON)
    assert response.status_code == 404


def test_water_requires_auth(app_server, seeded_db):
    response = requests.post(
        f"{BASE_URL}/game/farm/alice_test/water", data={"slot": 0}, headers=JSON
    )
    assert response.status_code == 401


def test_water_neighbour_build(app_server, seeded_db):
    alice = _reset_farm("alice_test")
    store.plant({"uid": alice["uid"], "username": "alice_test"}, 0, "python")
    session, _ = _signup()
    response = session.post(
        f"{BASE_URL}/game/farm/alice_test/water", data={"slot": 0}, headers=JSON
    )
    assert response.status_code == 200
    assert response.json()["farm"]["plots"][0]["watered_count"] == 1


def test_water_own_build_returns_400(app_server, seeded_db):
    alice = _reset_farm("alice_test")
    store.plant({"uid": alice["uid"], "username": "alice_test"}, 0, "python")
    session = requests.Session()
    session.post(
        f"{BASE_URL}/auth/login",
        data={"email": "alice@test.devplace", "password": "secret123"},
        allow_redirects=True,
    )
    response = session.post(
        f"{BASE_URL}/game/farm/alice_test/water", data={"slot": 0}, headers=JSON
    )
    assert response.status_code == 400
