# retoor <retoor@molodetz.nl>

import time
import requests
from tests.conftest import BASE_URL
from devplacepy.database import get_table
JSON_content_negotiation = {"Accept": "application/json"}
_counter_content_negotiation = [0]
def _session_content_negotiation(password="secret123"):
    _counter_content_negotiation[0] += 1
    name = f"cn{int(time.time() * 1000)}{_counter_content_negotiation[0]}"
    s = requests.Session()
    s.post(
        f"{BASE_URL}/auth/signup",
        data={
            "username": name,
            "email": f"{name}@t.dev",
            "password": password,
            "confirm_password": password,
            "birth_date": "1990-01-01",
            "accept_terms": "1",
        },
        allow_redirects=True,
    )
    return s, name
PUBLIC_PAGES = ["/feed", "/projects", "/gists", "/news", "/leaderboard", "/issues"]


def test_public_pages_serve_json_and_html(app_server):
    for path in PUBLIC_PAGES:
        rj = requests.get(f"{BASE_URL}{path}", headers=JSON_content_negotiation)
        assert rj.status_code == 200, path
        assert rj.headers["content-type"].startswith("application/json"), path
        assert isinstance(rj.json(), dict), path
        rh = requests.get(f"{BASE_URL}{path}")
        assert rh.status_code == 200, path
        assert rh.headers["content-type"].startswith("text/html"), path


def test_authed_pages_serve_json_and_html(app_server):
    s, name = _session_content_negotiation()
    for path in ["/messages", "/notifications", "/bookmarks/saved", f"/profile/{name}"]:
        rj = s.get(f"{BASE_URL}{path}", headers=JSON_content_negotiation)
        assert rj.status_code == 200, path
        assert rj.headers["content-type"].startswith("application/json"), path
        rh = s.get(f"{BASE_URL}{path}")
        assert rh.status_code == 200, path
        assert rh.headers["content-type"].startswith("text/html"), path
