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

import events.reenroll_if_failed as app


def test_reenroll_custom_dedup_window(
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    lambda_context: LambdaContext,
):
    enrollment_id = '294e9864-8284-4287-b153-927b15d90900'
    event = {
        'detail': {
            'new_image': {
                'id': enrollment_id,
                'sk': '0',
                'user': {
                    'id': '2beb8642-aab4-4088-86d4-2966fac7c570',
                    'name': 'Gary Pihl',
                    'email': 'gary@boston.com',
                    'cpf': '83152103000',
                },
                'course': {
                    'id': '62bea9b7-971d-4ee8-ab56-942dc7ca6fcd',
                    'name': 'pytest',
                },
            }
        }
    }
    assert app.lambda_handler(event, lambda_context)  # type: ignore

    # Parent knows the child
    r = dynamodb_persistence_layer.collection.query(
        KeyPair(
            pk=enrollment_id,
            sk='LINKED_ENTITIES#CHILD',
        )
    )
    *_, child_id = r['items'][0]['sk'].split('#')

    # Child knows the parent
    child = dynamodb_persistence_layer.collection.get_item(
        KeyPair(
            pk=child_id,
            sk=f'LINKED_ENTITIES#PARENT#ENROLLMENT#{enrollment_id}',
        )
    )
    assert child

    dedup_window = dynamodb_persistence_layer.collection.get_item(
        KeyPair(child_id, 'METADATA#DEDUPLICATION_WINDOW')
    )
    assert dedup_window
