from aws_lambda_powertools.event_handler.api_gateway import Router
from layercake.dynamodb import (
    DynamoDBPersistenceLayer,
    KeyPair,
)
from pydantic import BaseModel

from boto3clients import dynamodb_client
from config import ENROLLMENT_TABLE

router = Router()
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)


class DeduplicationWindow(BaseModel):
    lock_hash: str


@router.patch(
    '/<id>/deduplicationwindow',
    compress=True,
    tags=['Enrollment'],
)
def deduplication_window(id: str, payload: DeduplicationWindow):
    with enrollment_layer.transact_writer() as transact:
        transact.delete(key=KeyPair(id, 'lock'))
        transact.delete(key=KeyPair('lock', payload.lock_hash))

    return payload
