"""تست‌های مدیریت و resolve دامنه."""
from __future__ import annotations


async def _create_tenant(client, slug="domainco"):
    resp = await client.post("/api/v1/tenants", json={"name": "Domain Co", "slug": slug})
    return resp.json()["id"]


async def test_create_domain(client):
    tenant_id = await _create_tenant(client)
    resp = await client.post(
        f"/api/v1/tenants/{tenant_id}/domains",
        json={"domain": "shop.example.com", "domain_type": "custom_domain"},
    )
    assert resp.status_code == 201
    data = resp.json()
    assert data["domain"] == "shop.example.com"
    assert data["tenant_id"] == tenant_id


async def test_resolve_domain(client):
    tenant_id = await _create_tenant(client, slug="resolveco")
    await client.post(
        f"/api/v1/tenants/{tenant_id}/domains",
        json={"domain": "resolve.example.com"},
    )
    resp = await client.post(
        "/api/v1/domains/resolve", json={"domain": "resolve.example.com"}
    )
    assert resp.status_code == 200
    data = resp.json()
    assert data["tenant_id"] == tenant_id
    assert data["tenant_slug"] == "resolveco"


async def test_resolve_unknown_domain_returns_404(client):
    resp = await client.post(
        "/api/v1/domains/resolve", json={"domain": "nope.example.com"}
    )
    assert resp.status_code == 404
