from http import HTTPMethod, HTTPStatus

from layercake.dynamodb import DynamoDBPersistenceLayer
from requests_toolbelt import MultipartEncoder

from ..conftest import HttpApiProxy, LambdaContext


def test_get_course(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/courses/2a8963fc-4694-4fe2-953a-316d1b10f1f5',
            method=HTTPMethod.GET,
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK


def test_edit_course(
    app,
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    course_id = '2a8963fc-4694-4fe2-953a-316d1b10f1f5'

    with open('tests/sample.html', 'rb') as f:
        m = MultipartEncoder(
            fields={
                'given_cert': 'true',
                'name': 'pytest updated from test',
                'access_period': '365',
                'rawfile': ('sample.html', f, 'text/html'),
            }
        )
        r = app.lambda_handler(
            http_api_proxy(
                raw_path=f'/courses/{course_id}',
                method=HTTPMethod.PUT,
                headers={
                    'Content-Type': m.content_type,
                },
                body=m.to_string().decode(),
                is_base64_encoded=True,
            ),
            lambda_context,
        )
        assert r['statusCode'] == HTTPStatus.NO_CONTENT

    r = dynamodb_persistence_layer.get_item(
        key={'id': course_id, 'sk': '0'},
    )

    assert (
        r['cert']['s3_uri']
        == 's3://saladeaula.digital/certs/templates/2a8963fc-4694-4fe2-953a-316d1b10f1f5.html'
    )


def test_sample(
    app,
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/courses/2a8963fc-4694-4fe2-953a-316d1b10f1f5/sample',
            method=HTTPMethod.POST,
            body={
                's3_uri': 's3://saladeaula.digital/certs/samples/cipa-grau-de-risco-1.html',
            },
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK


def test_template(
    app,
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/courses/2a8963fc-4694-4fe2-953a-316d1b10f1f5/template',
            method=HTTPMethod.POST,
            body={
                's3_uri': 's3://saladeaula.digital/certs/samples/cipa-grau-de-risco-1.html',
            },
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK


def test_get_scormset(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/courses/c27d1b4f-575c-4b6b-82a1-9b91ff369e0b/scormset/76c75561-d972-43ef-9818-497d8fc6edbe',
            method=HTTPMethod.GET,
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK
