from http import HTTPMethod, HTTPStatus

from layercake.dynamodb import (
    ComposeKey,
    DynamoDBCollection,
    DynamoDBPersistenceLayer,
    KeyPair,
    PartitionKey,
)

from ..conftest import HttpApiProxy, LambdaContext


def test_enroll(
    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='/enrollments',
            method=HTTPMethod.POST,
            headers={'X-Tenant': 'cJtK9SsnJhKPyxESe7g3DG'},
            body={
                'items': [
                    {
                        'user': {
                            'id': '5OxmMjL-ujoR5IMGegQz',
                            'name': 'Sérgio R Siqueira',
                            'email': 'sergio@somosbeta.com.br',
                            'cpf': '07879819908',
                        },
                        'course': {
                            'id': '6d69a34a-cefd-40aa-a89b-dceb694c3e61',
                            'name': 'pytest',
                        },
                    },
                ],
            },
        ),
        lambda_context,
    )

    assert r['statusCode'] == HTTPStatus.OK
    print(r)


def test_vacancies(
    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='/enrollments/vacancies',
            method=HTTPMethod.GET,
            headers={'X-Tenant': '*'},
        ),
        lambda_context,
    )

    assert r['statusCode'] == HTTPStatus.OK


def test_cancel_enrollment(
    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='/enrollments/43ea4475-c369-4f90-b576-135b7df5106b/cancel',
            method=HTTPMethod.PATCH,
            headers={'X-Tenant': '*'},
            body={
                'id': '43ea4475-c369-4f90-b576-135b7df5106b',
                'lock_hash': 'f8f7996aa99d50eb85266be5a9fca1db',
                'course': {
                    'id': '123',
                    'name': 'NR-10',
                    'time_in_days': 720,
                },
                'vacancy': {
                    'sk': 'QV4sXY3DvSTUMGJ4QqsrwJ#43ea4475-c369-4f90-b576-135b7df5106b',
                    'id': 'vacancies#cJtK9SsnJhKPyxESe7g3DG',
                },
            },
        ),
        lambda_context,
    )

    assert r['statusCode'] == HTTPStatus.OK

    collect = DynamoDBCollection(dynamodb_persistence_layer)
    enrollment = collect.get_item(KeyPair('43ea4475-c369-4f90-b576-135b7df5106b', '0'))
    assert enrollment['status'] == 'CANCELED'

    vacancies = collect.query(
        PartitionKey(ComposeKey('cJtK9SsnJhKPyxESe7g3DG', 'vacancies'))
    )
    assert len(vacancies['items']) == 1
