"""Test utilities for waft_client.

Provides helpers for Playwright/BDD testing of apps that use waft_client.
In test mode (WAFT_AUTH_TEST_MODE=true), the library honors the X-Waft-Test-Auth
header to bypass real authentication.

Usage in Playwright tests:

    # Set the header that waft_client will honor in test mode
    context = browser.new_context(
        extra_http_headers={
            "X-Waft-Test-Auth": '{"user_id": "test-1", "email": "test@example.com"}'
        }
    )
    page = context.new_page()
    page.goto("http://localhost:8000/")
    # The app sees a logged-in user with id="test-1"

Environment setup:
    Set WAFT_AUTH_TEST_MODE=true when running tests.
    This env var is NEVER set in production waft deployments.
"""

from __future__ import annotations

import json


def test_auth_header(user_id: str = "test-user", email: str = "test@test.com") -> dict[str, str]:
    """Build the X-Waft-Test-Auth header dict for use in HTTP clients or Playwright.

    Returns a dict like {"X-Waft-Test-Auth": '{"user_id": "test-user", "email": "test@test.com"}'}
    """
    return {
        "X-Waft-Test-Auth": json.dumps({"user_id": user_id, "email": email})
    }


def make_test_event(
    path: str = "/",
    method: str = "GET",
    user_id: str = "test-user",
    email: str = "test@test.com",
    body: str = "",
    query_params: dict | None = None,
) -> dict:
    """Build a Lambda event dict with test auth header set.

    Useful for unit-testing Lambda handlers that use WaftAuth.get_user().
    """
    return {
        "rawPath": path,
        "requestContext": {"http": {"method": method}},
        "headers": {
            "x-waft-test-auth": json.dumps({"user_id": user_id, "email": email}),
        },
        "cookies": [],
        "queryStringParameters": query_params or {},
        "body": body,
        "isBase64Encoded": False,
    }
