"""
Smoke tests: app boots, routers wire up, no import-time crashes.

The router-list test asserts each endpoint returns 200 or 401 (auth gate not
yet implemented in R1-A's scope) but explicitly NOT 5xx — the goal is to catch
import errors and crashing handlers, not to validate business logic.
"""

from __future__ import annotations

import os
import pytest


def test_app_imports():
    from app.main import app
    assert app.title == "MealPlanner"


@pytest.mark.requires_postgres
def test_health(client):
    r = client.get("/health")
    assert r.status_code == 200
    assert r.json() == {"status": "ok"}


@pytest.mark.requires_postgres
def test_health_db(client):
    r = client.get("/health/db")
    assert r.status_code == 200
    body = r.json()
    assert body.get("database") == "connected"


ROUTER_GET_PATHS = [
    "/api/profile",
    "/api/profile/members",
    "/api/recipes",
    "/api/recipes/ingredients",
    "/api/meals",
    "/api/pantry",
    "/api/shopping-list",
    "/api/admin/logs",
]


@pytest.mark.requires_postgres
@pytest.mark.parametrize("path", ROUTER_GET_PATHS)
def test_router_list_endpoints(client, path):
    """
    Each canonical GET must respond. 200 (handled), 401 (admin-gated),
    404 (handler ran but no row found) — all OK. 5xx means import/runtime
    crash; 307 (trailing-slash redirect) means a handler is still mounted
    on the wrong path.
    """
    r = client.get(path, follow_redirects=False)
    assert r.status_code < 500, (
        f"{path} returned {r.status_code}: {r.text[:300]}"
    )
    # 307 is a regression — canonical paths must be the route definitions.
    assert r.status_code in (200, 401, 404, 422), (
        f"{path} returned unexpected {r.status_code}"
    )
