from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import (
    DynamoDBPersistenceLayer,
    SortKey,
    TransactKey,
)

import events.billing.close_window as app


def test_close_window(
    dynamodb_seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    lambda_context: LambdaContext,
):
    event = {
        'detail': {
            'keys': {
                'id': 'BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG',
                'sk': 'START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE',
            },
        }
    }

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

    r = dynamodb_persistence_layer.collection.get_items(
        TransactKey('BILLING#ORG#cJtK9SsnJhKPyxESe7g3DG')
        + SortKey('START#2025-07-01#END#2025-07-31')
        + SortKey('START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE#EXECUTED'),
        flatten_top=False,
    )

    assert 's3_uri' in r['START#2025-07-01#END#2025-07-31']
    assert 'created_at' in r['START#2025-07-01#END#2025-07-31']
    assert 'updated_at' in r['START#2025-07-01#END#2025-07-31']
    assert r['START#2025-07-01#END#2025-07-31']['status'] == 'CLOSED'
    assert 'START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE#EXECUTED' in r
