# retoor <retoor@molodetz.nl>

from datetime import datetime, timezone
from devplacepy.database import get_table, invalidate_admins_cache
from devplacepy.config import DEFAULT_CORRECTION_PROMPT, DEFAULT_MODIFIER_PROMPT
from devplacepy.utils.text import generate_uid
from devplacepy.utils.badges import award_badge
from devplacepy.utils.passwords import hash_password, hash_password_async


def _create_account(
    username: str,
    email: str,
    password_hash: str,
    *,
    age_band: str = "",
    accepted_terms: bool = False,
) -> tuple[str, str, bool]:
    from devplacepy.database import get_setting, set_consent

    users = get_table("users")
    uid = generate_uid()
    is_first = users.count() == 0
    role = "Admin" if is_first else "Member"
    now = datetime.now(timezone.utc).isoformat()
    terms_version = (get_setting("terms_version", "1") or "1") if accepted_terms else ""
    users.insert(
        {
            "uid": uid,
            "username": username,
            "email": email.strip().lower(),
            "api_key": generate_uid(),
            "password_hash": password_hash,
            "bio": "",
            "location": "",
            "git_link": "",
            "website": "",
            "role": role,
            "is_active": True,
            "level": 1,
            "xp": 0,
            "stars": 0,
            "ai_correction_enabled": 0,
            "ai_correction_prompt": DEFAULT_CORRECTION_PROMPT,
            "ai_correction_sync": 0,
            "ai_modifier_enabled": 1,
            "ai_modifier_prompt": DEFAULT_MODIFIER_PROMPT,
            "ai_modifier_sync": 1,
            "interactions_enabled": -1,
            "avatar_seed": None,
            "terms_version": terms_version,
            "terms_accepted_at": now if accepted_terms else "",
            "age_band": age_band,
            "age_declared_at": now if age_band else "",
            "mature_opt_in": 0,
            "suspended_until": "",
            "suspension_reason": "",
            "deletion_requested_at": "",
            "created_at": now,
        }
    )
    if accepted_terms:
        set_consent("user", uid, "terms", True, version=terms_version)
        set_consent(
            "user", uid, "privacy", True, version=get_setting("privacy_version", "1") or "1"
        )
        set_consent("user", uid, "activity_recording", True)
    award_badge(uid, "Member")
    if is_first:
        invalidate_admins_cache()
    return uid, role, is_first


def register_account(
    username: str,
    email: str,
    password: str,
    *,
    age_band: str = "",
    accepted_terms: bool = False,
) -> tuple[str, str, bool]:
    return _create_account(
        username,
        email,
        hash_password(password),
        age_band=age_band,
        accepted_terms=accepted_terms,
    )


async def register_account_async(
    username: str,
    email: str,
    password: str,
    *,
    age_band: str = "",
    accepted_terms: bool = False,
) -> tuple[str, str, bool]:
    return _create_account(
        username,
        email,
        await hash_password_async(password),
        age_band=age_band,
        accepted_terms=accepted_terms,
    )
