import json
from http import HTTPMethod, HTTPStatus

from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair

from ...conftest import HttpApiProxy, LambdaContext


def test_get_emails(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path="/users/15bacf02-1535-4bee-9022-19d106fd7518/emails",
            method=HTTPMethod.GET,
        ),
        lambda_context,
    )
    assert r["statusCode"] == HTTPStatus.OK

    body = json.loads(r["body"])
    assert len(body["items"]) == 2


def test_add_email(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path="/users/15bacf02-1535-4bee-9022-19d106fd7518/emails",
            method=HTTPMethod.POST,
            body={
                "email": "osergiosiqueira+pytest@gmail.com",
            },
        ),
        lambda_context,
    )

    assert r["statusCode"] == HTTPStatus.CREATED
    r = dynamodb_persistence_layer.collection.query(
        KeyPair(
            "15bacf02-1535-4bee-9022-19d106fd7518",
            "EMAIL_VERIFICATION",
        )
    )
    items = r["items"]

    assert len(items) == 2
    assert any(x.get("email") == "osergiosiqueira+pytest@gmail.com" for x in items)


def test_email_as_primary(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path="/users/15bacf02-1535-4bee-9022-19d106fd7518/emails/primary",
            method=HTTPMethod.PATCH,
            body={
                "old_email": "sergio@somosbeta.com.br",
                "new_email": "osergiosiqueira@gmail.com",
                "email_verified": "false",
            },
        ),
        lambda_context,
    )

    assert r["statusCode"] == HTTPStatus.NO_CONTENT

    r = dynamodb_persistence_layer.collection.get_item(
        KeyPair("15bacf02-1535-4bee-9022-19d106fd7518", "0")
    )
    assert r["email"] == "osergiosiqueira@gmail.com"
    assert r["emails"] == {"osergiosiqueira@gmail.com", "sergio@somosbeta.combr"}


def test_verify_email(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path="/users/15bacf02-1535-4bee-9022-19d106fd7518/emails/0d29c753-55f8-42d2-908b-e4976aafc183/verify",
            method=HTTPMethod.POST,
        ),
        lambda_context,
    )

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


def test_verify_email_notfound(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path="/users/15bacf02-1535-4bee-9022-19d106fd7518/emails/abc/verify",
            method=HTTPMethod.POST,
        ),
        lambda_context,
    )

    assert r["statusCode"] == HTTPStatus.NOT_FOUND


def test_remove_emal(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path="/users/15bacf02-1535-4bee-9022-19d106fd7518/emails/osergiosiqueira@gmail.com",
            method=HTTPMethod.DELETE,
        ),
        lambda_context,
    )

    assert r["statusCode"] == HTTPStatus.NO_CONTENT
