from tests.test_auth import _signup

PHONE = "0912345678"


def _auth_headers(token: str) -> dict:
    return {"Authorization": f"Bearer {token}"}


def _customer_token(client, phone=PHONE):
    return _signup(client, phone=phone)["access_token"]


def test_profile_starts_empty(client):
    token = _customer_token(client)
    resp = client.get("/api/v1/customer/profile", headers=_auth_headers(token))
    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["gender"] is None
    assert body["nationality"] is None
    assert body["date_of_birth"] is None
    assert body["default_language"] is None
    assert body["address"] is None
    assert body["default_delivery_address"] is None


def test_profile_update_is_partial(client):
    token = _customer_token(client)
    headers = _auth_headers(token)

    resp = client.patch(
        "/api/v1/customer/profile",
        json={"gender": "female", "nationality": "Ethiopian"},
        headers=headers,
    )
    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["gender"] == "female"
    assert body["nationality"] == "Ethiopian"
    assert body["date_of_birth"] is None  # untouched

    resp = client.patch(
        "/api/v1/customer/profile",
        json={"date_of_birth": "1998-05-14"},
        headers=headers,
    )
    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["date_of_birth"] == "1998-05-14"
    # Fields from the first PATCH are still there — untouched, not reset.
    assert body["gender"] == "female"
    assert body["nationality"] == "Ethiopian"


def test_profile_default_delivery_address_matches_order_shape(client):
    """default_delivery_* mirrors Order's own delivery_address/
    delivery_latitude/delivery_longitude fields on purpose, so a
    checkout screen can prefill straight from this row."""
    token = _customer_token(client)
    resp = client.patch(
        "/api/v1/customer/profile",
        json={
            "default_delivery_address": "Bole, Addis Ababa",
            "default_delivery_latitude": 9.005,
            "default_delivery_longitude": 38.763,
        },
        headers=_auth_headers(token),
    )
    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["default_delivery_address"] == "Bole, Addis Ababa"
    assert body["default_delivery_latitude"] == 9.005
    assert body["default_delivery_longitude"] == 38.763


def test_profile_rejects_invalid_latitude(client):
    token = _customer_token(client)
    resp = client.patch(
        "/api/v1/customer/profile",
        json={"default_delivery_latitude": 999},
        headers=_auth_headers(token),
    )
    assert resp.status_code == 422


def test_profile_requires_authentication(client):
    resp = client.get("/api/v1/customer/profile")
    assert resp.status_code == 401  # no Authorization header at all


def test_profile_is_scoped_to_the_logged_in_customer(client):
    token_a = _customer_token(client, phone="0911111111")
    token_b = _customer_token(client, phone="0922222222")

    client.patch(
        "/api/v1/customer/profile",
        json={"gender": "male"},
        headers=_auth_headers(token_a),
    )

    resp = client.get("/api/v1/customer/profile", headers=_auth_headers(token_b))
    assert resp.json()["gender"] is None
