import json
from http import HTTPMethod, HTTPStatus

from layercake.dynamodb import ComposeKey, DynamoDBCollection, PartitionKey

from ..conftest import HttpApiProxy, LambdaContext


def test_post_course(
    mock_app,
    dynamodb_persistence_layer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = mock_app.lambda_handler(
        http_api_proxy(
            raw_path='/courses',
            method=HTTPMethod.POST,
            headers={'X-Tenant': '*'},
            body={
                'name': 'pytest',
                'access_period': 365,
                'cert': {
                    'exp_interval': 730,  # 2 years
                },
            },
        ),
        lambda_context,
    )

    # print(r)

    assert 'id' in json.loads(r['body'])
    assert r['statusCode'] == HTTPStatus.CREATED

    collect = DynamoDBCollection(dynamodb_persistence_layer)
    logs = collect.get_items(
        PartitionKey(ComposeKey('5OxmMjL-ujoR5IMGegQz', prefix='logs'))
    )
    print(logs)
