"""Tests for admin API key management."""

from __future__ import annotations


def test_create_api_key(client, admin_headers):
    resp = client.post(
        "/admin/api-keys",
        json={"org_name": "Acme Corp"},
        headers=admin_headers,
    )
    assert resp.status_code == 201
    data = resp.json()
    assert data["org_name"] == "Acme Corp"
    assert data["api_key"].startswith("pt_live_")
    assert data["id"].startswith("pk_")


def test_create_key_missing_secret(client):
    resp = client.post("/admin/api-keys", json={"org_name": "Acme"})
    assert resp.status_code == 422


def test_create_key_wrong_secret(client):
    resp = client.post(
        "/admin/api-keys",
        json={"org_name": "Acme"},
        headers={"X-Admin-Secret": "wrong"},
    )
    assert resp.status_code == 403


def test_created_key_works(client, admin_headers):
    resp = client.post(
        "/admin/api-keys",
        json={"org_name": "NewOrg"},
        headers=admin_headers,
    )
    new_key = resp.json()["api_key"]

    # Use the new key to hit a protected endpoint
    resp = client.get("/receipts", headers={"X-API-Key": new_key})
    assert resp.status_code == 200
