"""تست‌های مدیریت Tenant."""
from __future__ import annotations


async def _create_tenant(client, slug="acme", name="Acme Inc"):
    return await client.post("/api/v1/tenants", json={"name": name, "slug": slug})


async def test_create_tenant(client):
    resp = await _create_tenant(client)
    assert resp.status_code == 201
    data = resp.json()
    assert data["slug"] == "acme"
    assert data["status"] == "active"
    assert "id" in data


async def test_create_tenant_normalizes_slug(client):
    resp = await _create_tenant(client, slug="Acme-Co")
    assert resp.status_code == 201
    assert resp.json()["slug"] == "acme-co"


async def test_slug_must_be_unique(client):
    first = await _create_tenant(client, slug="dup")
    assert first.status_code == 201
    second = await _create_tenant(client, slug="dup")
    assert second.status_code == 409
    assert second.json()["error"]["code"] == "slug_taken"


async def test_get_and_list_tenants(client):
    created = await _create_tenant(client, slug="listme")
    tenant_id = created.json()["id"]

    got = await client.get(f"/api/v1/tenants/{tenant_id}")
    assert got.status_code == 200
    assert got.json()["id"] == tenant_id

    listed = await client.get("/api/v1/tenants")
    assert listed.status_code == 200
    body = listed.json()
    assert body["meta"]["total_items"] >= 1
    assert len(body["items"]) >= 1


async def test_suspend_and_activate(client):
    created = await _create_tenant(client, slug="susp")
    tenant_id = created.json()["id"]

    suspended = await client.post(f"/api/v1/tenants/{tenant_id}/suspend")
    assert suspended.status_code == 200
    assert suspended.json()["status"] == "suspended"

    activated = await client.post(f"/api/v1/tenants/{tenant_id}/activate")
    assert activated.status_code == 200
    assert activated.json()["status"] == "active"
