import json
from http import HTTPMethod, HTTPStatus

from layercake.dynamodb import DynamoDBPersistenceLayer, SortKey, TransactKey

from ..conftest import HttpApiProxy, LambdaContext


def test_preexisting_user(
    app,
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/register',
            method=HTTPMethod.POST,
            body={
                'id': '357db1c5-7442-4075-98a3-fbe5c938a419',
                'name': 'Sérgio R Siqueira',
                'cpf': '07879819908',
                'password': 'Led@Zepellin',
                'email': 'sergio@somosbeta.com.br',
                'never_logged': 'true',
            },
        ),
        lambda_context,
    )

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


def test_preexisting_user_conflict(
    app,
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/register',
            method=HTTPMethod.POST,
            body={
                'id': '357db1c5-7442-4075-98a3-fbe5c938a419',
                'name': 'Sérgio R Siqueira',
                'cpf': '07879819908',
                'password': 'Led@Zepellin',
                'email': 'osergiosiqueira@gmail.com',
            },
        ),
        lambda_context,
    )
    body = json.loads(r['body'])

    assert body['type'] == 'EmailConflictError'
    assert r['statusCode'] == HTTPStatus.CONFLICT


def test_preexisting_user_update_email(
    app,
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/register',
            method=HTTPMethod.POST,
            body={
                'id': '357db1c5-7442-4075-98a3-fbe5c938a419',
                'name': 'Sérgio R Siqueira',
                'cpf': '07879819908',
                'password': 'Led@Zepellin',
                'email': 'osergiosiqueira+pytest@gmail.com',
            },
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK
    assert 'cookies' in r

    user = dynamodb_persistence_layer.collection.get_items(
        TransactKey(
            '357db1c5-7442-4075-98a3-fbe5c938a419',
        )
        + SortKey('0')
        + SortKey('emails#osergiosiqueira+pytest@gmail.com')
        + SortKey('emails#sergio@somosbeta.com.br'),
        flatten_top=False,
    )
    assert user['0']['email'] == 'osergiosiqueira+pytest@gmail.com'
    assert 'emails#osergiosiqueira+pytest@gmail.com' in user
    assert 'emails#sergio@somosbeta.com.br' in user
    assert user['0']['emails'] == {
        'sergio@somosbeta.com.br',
        'osergiosiqueira+pytest@gmail.com',
    }


def test_non_preexisting_user(
    app,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/register',
            method=HTTPMethod.POST,
            body={
                # 'id': '14ddcef6-483c-4181-bdb2-3e9a31a24732',
                'name': 'David Bowie',
                'cpf': '23355097055',
                'password': 'Ziggy@Stardust',
                'email': 'david@bowie.com',
            },
        ),
        lambda_context,
    )

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