"""Public Portal self-registration captures a phone number (required by
default, validated) and stores it on the new account."""


def _payload(tenant_id, **overrides):
    return {
        "tenant_id": str(tenant_id),
        "organization_name": "Phone Test Org",
        "name": "Reg Phone",
        "email": "reg-phone@example.com",
        "password": "register-123",
        "phone": " +91 98470 12345 ",
        **overrides,
    }


async def test_registration_stores_phone(client, auth_headers, test_tenant):
    resp = await client.post("/auth/register", json=_payload(test_tenant["tenant_id"]))
    assert resp.status_code == 201, resp.text
    users = (await client.get("/users", headers=auth_headers)).json()
    user = next(u for u in users if u["email"] == "reg-phone@example.com")
    assert user["phone"] == "+91 98470 12345"


async def test_registration_requires_a_valid_phone_by_default(client, test_tenant):
    tenant_id = test_tenant["tenant_id"]
    missing = _payload(tenant_id)
    del missing["phone"]
    resp = await client.post("/auth/register", json=missing)
    assert resp.status_code == 400 and "Phone number" in resp.json()["detail"]
    assert (await client.post("/auth/register", json=_payload(tenant_id, phone="  "))).status_code == 400
    assert (await client.post("/auth/register", json=_payload(tenant_id, phone="call me"))).status_code == 422
