import json
from http import HTTPMethod, HTTPStatus

from layercake.dynamodb import DynamoDBCollection, DynamoDBPersistenceLayer, KeyPair

from ..conftest import HttpApiProxy, LambdaContext


def test_get_policies(
    mock_app,
    dynamodb_seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = mock_app.lambda_handler(
        http_api_proxy(
            raw_path='/orgs/cJtK9SsnJhKPyxESe7g3DG/policies',
            method=HTTPMethod.GET,
            headers={'X-Tenant': '*'},
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK
    assert json.loads(r['body']) == {
        'billing_policy': {'billing_day': 1, 'payment_method': 'PIX'},
        'payment_policy': {'due_days': 90},
    }


def test_put_policies(
    mock_app,
    dynamodb_seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = mock_app.lambda_handler(
        http_api_proxy(
            raw_path='/orgs/cJtK9SsnJhKPyxESe7g3DG/policies',
            method=HTTPMethod.PUT,
            headers={'X-Tenant': '*'},
            body={'payment_policy': None},
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK

    collect = DynamoDBCollection(dynamodb_persistence_layer)
    course = collect.get_item(KeyPair('cJtK9SsnJhKPyxESe7g3DG', '0'))
    assert course['name'] == 'EDUSEG'
