import json
from http import HTTPMethod, HTTPStatus

from layercake.dynamodb import 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,
    )
    expected = {
        'billing_policy': {'billing_day': 1, 'payment_method': 'PIX'},
        'payment_policy': {'due_days': 90},
    }

    assert r['statusCode'] == HTTPStatus.OK
    assert json.loads(r['body']) == expected


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

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


def test_get_address(
    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/address',
            method=HTTPMethod.GET,
            body={'payment_policy': None},
        ),
        lambda_context,
    )
    expected = {
        'id': 'cJtK9SsnJhKPyxESe7g3DG',
        'sk': 'metadata#address',
        'postcode': '88101001',
        'address1': 'Av. Presidente Kennedy, 815',
        'address2': 'Sala 1',
        'neighborhood': 'Campinas',
        'city': 'São José',
        'state': 'SC',
    }
    assert r['statusCode'] == HTTPStatus.OK
    assert json.loads(r['body']) == expected


def test_post_address(
    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/address',
            method=HTTPMethod.POST,
            body={
                'postcode': '81280-350',
                'address1': 'Rua Monsenhor Ivo Zanlorenzi, 5190 ',
                'address2': ' ap 1802',
                'neighborhood': 'Cidade Industrial',
                'city': 'Curitiba',
                'state': 'PR',
            },
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK

    data = dynamodb_persistence_layer.collection.get_item(
        KeyPair('cJtK9SsnJhKPyxESe7g3DG', 'metadata#address')
    )
    assert data['address1'] == 'Rua Monsenhor Ivo Zanlorenzi, 5190'
    assert data['postcode'] == '81280350'


def test_get_custom_pricing(
    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/custompricing',
            method=HTTPMethod.GET,
        ),
        lambda_context,
    )

    expected = {
        'items': [
            {
                'sk': 'COURSE#281198c2-f293-4acc-b96e-e4a2d5f6b73c',
                'unit_price': 199,
                'id': 'CUSTOM_PRICING#ORG#cJtK9SsnJhKPyxESe7g3DG',
            }
        ],
        'last_key': None,
    }
    assert json.loads(r['body']) == expected


def test_delete_custom_pricing(
    mock_app,
    dynamodb_seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    mock_app.lambda_handler(
        http_api_proxy(
            raw_path='/orgs/cJtK9SsnJhKPyxESe7g3DG/custompricing',
            method=HTTPMethod.DELETE,
            body={'course_id': '281198c2-f293-4acc-b96e-e4a2d5f6b73c'},
        ),
        lambda_context,
    )

    data = dynamodb_persistence_layer.collection.get_item(
        KeyPair(
            'CUSTOM_PRICING#ORG#cJtK9SsnJhKPyxESe7g3DG',
            'COURSE#15ee05a3-4ceb-4b7e-9979-db75b28c9ade',
        ),
        raise_on_error=False,
    )

    assert not data


def test_post_custom_pricing(
    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/custompricing',
            method=HTTPMethod.POST,
            body={
                'course_id': '15ee05a3-4ceb-4b7e-9979-db75b28c9ade',
                'unit_price': '99.3',
            },
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.CREATED

    data = dynamodb_persistence_layer.collection.get_item(
        KeyPair(
            'CUSTOM_PRICING#ORG#cJtK9SsnJhKPyxESe7g3DG',
            'COURSE#15ee05a3-4ceb-4b7e-9979-db75b28c9ade',
        )
    )

    assert data
