"""Storage Dashboard backend smoke tests — GET /api/storage/dashboard."""
import os
import requests

BASE_URL = os.environ.get("REACT_APP_BACKEND_URL", "https://offline-billing-pro-2.preview.emergentagent.com").rstrip("/")


def test_storage_dashboard_requires_auth():
    """Unauthenticated should fail."""
    r = requests.get(f"{BASE_URL}/api/storage/dashboard", timeout=30)
    assert r.status_code in (401, 403), f"Expected 401/403 got {r.status_code}"


def test_storage_dashboard_with_admin(admin_session):
    """Authenticated admin returns 200 with required keys."""
    r = admin_session.get(f"{BASE_URL}/api/storage/dashboard", timeout=30)
    assert r.status_code == 200, r.text
    data = r.json()
    # Top-level keys
    for key in ("local", "backups", "drive", "sync", "fetched_at"):
        assert key in data, f"Missing key {key}"
    # Local
    for k in ("objects_count", "data_size_mb", "storage_size_mb", "collections"):
        assert k in data["local"]
    assert isinstance(data["local"]["objects_count"], int)
    assert isinstance(data["local"]["collections"], int)
    # Backups
    for k in ("count", "latest_at", "latest_label", "total_size_mb", "drive_synced_count"):
        assert k in data["backups"]
    assert isinstance(data["backups"]["count"], int)
    # Drive
    for k in ("connected", "account_email", "auto_sync", "last_push_at"):
        assert k in data["drive"]
    assert isinstance(data["drive"]["connected"], bool)
    # Sync
    for k in ("pending_uploads", "last_sync_at"):
        assert k in data["sync"]
    assert isinstance(data["sync"]["pending_uploads"], int)


def test_invoice_exists_for_sharepanel_test(admin_session):
    """Verify the seed invoice id referenced in the review request is loadable."""
    inv_id = "6a1af7ef367a0252e0545e15"
    r = admin_session.get(f"{BASE_URL}/api/invoices/{inv_id}", timeout=30)
    assert r.status_code == 200, f"Invoice {inv_id} not found: {r.status_code} {r.text[:200]}"
    data = r.json()
    assert data.get("invoice_no"), "invoice_no missing"
