"""Integration coverage for the Passwordless module."""

from __future__ import annotations

import time

import pytest

pytestmark = [pytest.mark.integration, pytest.mark.template_integration]


def test_describe_passwordless_metadata() -> None:
    from src.modules.free.auth.passwordless.passwordless import (
        PasswordlessRuntime,
        describe_passwordless,
        list_passwordless_features,
        load_passwordless_settings,
    )

    settings = load_passwordless_settings()
    runtime = PasswordlessRuntime(settings)

    metadata = runtime.metadata()
    assert metadata["module"] == "passwordless"
    assert metadata["token_ttl_seconds"] == settings.token_ttl_seconds
    assert set(metadata["features"]).issuperset(set(list_passwordless_features()))

    described = describe_passwordless(settings)
    assert described["supports_magic_links"] is True
    assert "email" in described["delivery_methods"]


def test_fastapi_passwordless_routes() -> None:
    from fastapi import FastAPI, status
    from fastapi.testclient import TestClient

    from src.modules.free.auth.passwordless.routers.passwordless import create_router

    app = FastAPI()
    app.include_router(create_router())
    client = TestClient(app)

    metadata_response = client.get("/passwordless/metadata")
    assert metadata_response.status_code == status.HTTP_200_OK
    metadata = metadata_response.json()
    assert metadata["supports_codes"] is True

    features_response = client.get("/passwordless/features")
    assert features_response.status_code == status.HTTP_200_OK
    features = features_response.json()["features"]
    assert "magic_links" in features

    token_response = client.post(
        "/passwordless/tokens",
        json={"identifier": "user@example.com", "delivery_method": "email"},
    )
    assert token_response.status_code == status.HTTP_201_CREATED
    token_payload = token_response.json()
    expires_at = token_payload["expires_at"]

    magic_link_response = client.post(
        "/passwordless/magic-link",
        json={"identifier": "link-user@example.com", "delivery_method": "email"},
    )
    assert magic_link_response.status_code == status.HTTP_201_CREATED
    assert magic_link_response.json()["url"].startswith("https://")

    verify_response = client.post(
        "/passwordless/verify",
        json={
            "identifier": "user@example.com",
            "code": token_payload["code"],
            "delivery_method": "email",
        },
    )
    assert verify_response.status_code == status.HTTP_200_OK
    verified_payload = verify_response.json()
    assert verified_payload["identifier"] == "user@example.com"

    assert expires_at >= time.time()


def test_passwordless_health_router() -> None:
    from fastapi import FastAPI, status
    from fastapi.testclient import TestClient

    from src.health.passwordless import register_passwordless_health

    app = FastAPI()
    register_passwordless_health(app)
    client = TestClient(app)

    response = client.get("/api/health/module/passwordless")
    assert response.status_code == status.HTTP_200_OK
    payload = response.json()
    assert payload["status"] == "ok"
    assert payload["token_ttl_seconds"] >= 1
