# retoor <retoor@molodetz.nl>

from datetime import datetime, timezone

import pytest

from devplacepy.database import get_table, purge, refresh_snapshot
from devplacepy.utils import generate_uid


@pytest.fixture(autouse=True)
def _seed_dbapi_schema(local_db):
    now = datetime.now(timezone.utc).isoformat()
    users = get_table("users")
    if "username" not in users.columns:
        users.insert(
            {
                "uid": generate_uid(),
                "username": "dbapi_seed",
                "terms_version": "1",
                "email": "seed@dbapi.test",
                "role": "Member",
                "xp": 0,
                "level": 1,
                "created_at": now,
            }
        )
    bookmarks = get_table("bookmarks")
    if "user_uid" not in bookmarks.columns:
        seed = generate_uid()
        bookmarks.insert(
            {
                "uid": seed,
                "user_uid": "_seedcols",
                "target_type": "post",
                "target_uid": "_s",
                "created_at": now,
                "deleted_at": None,
                "deleted_by": None,
            }
        )
        purge("bookmarks", uid=seed)
    refresh_snapshot()
    yield
