from aws_lambda_powertools.event_handler.api_gateway import Router
from aws_lambda_powertools.event_handler.exceptions import (
    BadRequestError as PowertoolsBadRequestError,
)
from layercake.dynamodb import (
    ComposeKey,
    DynamoDBCollection,
    DynamoDBPersistenceLayer,
    MissingError,
    PartitionKey,
)

from boto3clients import dynamodb_client
from config import USER_TABLE

from .orgs import router as orgs

__all__ = ['orgs']


class BadRequestError(MissingError, PowertoolsBadRequestError): ...


router = Router()
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
user_collect = DynamoDBCollection(user_layer, exc_cls=BadRequestError)


@router.get(
    '/<id>/logs',
    compress=True,
    tags=['User'],
    summary='Get user logs',
)
def get_logs(id: str):
    return user_collect.query(
        # Post-migration: uncomment to enable PartitionKey with a composite key (id with `logs` prefix).
        # PartitionKey(ComposeKey(id, 'logs')),
        PartitionKey(ComposeKey(id, 'log', delimiter=':')),
        start_key=router.current_event.get_query_string_value('start_key', None),
    )
