"""
preferred_locale kunne FØR kun settes ved førstegangs-signup (ADR-015)
-- aldri endres etterpå. ADR-094 la feltet til den allerede generiske
ProfileUpdate (PATCH /profile) slik at language-switcher.tsx kan endre
det når som helst."""

import pytest
from fastapi import HTTPException

from app.auth import CurrentUser
from app.routers.auth import ProfileUpdate, update_profile

from tests.conftest import create_user

import app.db as app_db


async def test_preferred_locale_can_be_changed_after_signup(pool):
    user_id = await create_user()
    out = await update_profile(
        ProfileUpdate(preferred_locale="en"),
        user=CurrentUser(user_id=user_id),
    )
    assert out.preferred_locale == "en"

    async with app_db.plain_connection() as conn:
        row = await conn.fetchrow("SELECT preferred_locale FROM app_user WHERE id = $1", user_id)
    assert row["preferred_locale"] == "en"


async def test_invalid_locale_is_rejected(pool):
    with pytest.raises(Exception):
        ProfileUpdate(preferred_locale="fr")


async def test_updating_other_fields_leaves_locale_unchanged(pool):
    user_id = await create_user()
    await update_profile(ProfileUpdate(preferred_locale="en"), user=CurrentUser(user_id=user_id))
    out = await update_profile(ProfileUpdate(bio="Elsker links-golf"), user=CurrentUser(user_id=user_id))
    assert out.preferred_locale == "en"
