from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair

import events.issue_cert as app


def test_issue_cert(
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    lambda_context: LambdaContext,
):
    enrollment_id = '1ee108ae-67d4-4545-bf6d-4e641cdaa4e0'
    event = {
        'detail': {
            'new_image': {
                'id': enrollment_id,
                'completed_at': '2025-09-21T14:20:36.276467-03:00',
                'started_at': '2025-09-19T14:34:54.704548-03:00',
                'user': {
                    'name': 'Jimi Hendrix',
                    'cpf': '74630003037',
                },
                'course': {
                    'id': '123',
                    'name': 'pytest',
                },
                'score': 79,
                'status': 'COMPLETED',
            }
        }
    }
    assert app.lambda_handler(event, lambda_context)  # type: ignore

    r = dynamodb_persistence_layer.get_item(
        key=KeyPair('1ee108ae-67d4-4545-bf6d-4e641cdaa4e0', '0')
    )

    assert 'cert' in r
    assert 'cert_expires_at' in r
    assert (
        r['cert']['s3_uri']
        == 's3://saladeaula.digital/certs/1ee108ae-67d4-4545-bf6d-4e641cdaa4e0.pdf'
    )


def test_non_exp_interval(
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    lambda_context: LambdaContext,
):
    enrollment_id = '1ee108ae-67d4-4545-bf6d-4e641cdaa4e0'
    event = {
        'detail': {
            'new_image': {
                'id': enrollment_id,
                'completed_at': '2025-09-21T14:20:36.276467-03:00',
                'started_at': '2025-09-19T14:34:54.704548-03:00',
                'user': {
                    'name': 'Josh Kiszka',
                    'cpf': '74630003037',
                },
                'course': {
                    'id': '12334',
                    'name': 'pytest',
                },
                'score': 79,
                'status': 'COMPLETED',
            }
        }
    }
    assert app.lambda_handler(event, lambda_context)  # type: ignore

    r = dynamodb_persistence_layer.get_item(
        key=KeyPair('1ee108ae-67d4-4545-bf6d-4e641cdaa4e0', '0')
    )

    assert 'cert' in r
    assert 'cert_expires_at' not in r
