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

import events.billing.append_enrollment as app


def test_append_enrollment(
    dynamodb_seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    lambda_context: LambdaContext,
):
    enrollment_id = '945e8672-1d72-45c6-b76c-ac06aa8b52ab'
    event = {
        'detail': {
            'new_image': {
                'id': enrollment_id,
                'sk': 'METADATA#SUBSCRIPTION_COVERED',
                'billing_day': 6,
                'created_at': '2025-07-23T18:09:22.785678-03:00',
                'org_id': 'cJtK9SsnJhKPyxESe7g3DG',
            }
        }
    }

    assert app.lambda_handler(event, lambda_context)  # type: ignore

    r = dynamodb_persistence_layer.collection.query(
        KeyPair('BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG', 'START#2025-05-06#END#2025-06-05')
    )
    items = r['items']

    assert items[0]['sk'] == 'START#2025-05-06#END#2025-06-05#SCHEDULE#AUTO_CLOSE'
    assert (
        items[1]['sk']
        == 'START#2025-05-06#END#2025-06-05#ENROLLMENT#945e8672-1d72-45c6-b76c-ac06aa8b52ab'
    )
    assert items[2]['sk'] == 'START#2025-05-06#END#2025-06-05'

    print(
        dynamodb_persistence_layer.collection.get_item(
            KeyPair(enrollment_id, 'METADATA#SUBSCRIPTION_COVERED')
        )
    )
