from aws_lambda_powertools.event_handler.api_gateway import Router
from layercake.dynamodb import (
    ComposeKey,
    DynamoDBPersistenceLayer,
    PartitionKey,
)

from boto3clients import dynamodb_client
from config import (
    ENROLLMENT_TABLE,
    USER_TABLE,
)
from middlewares import Tenant, TenantMiddleware

router = Router()

enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)


@router.get(
    '/slots',
    compress=True,
    tags=['Enrollment'],
    middlewares=[
        TenantMiddleware(user_layer.collection),
    ],
)
def get_vacancies():
    tenant: Tenant = router.context['tenant']

    return enrollment_layer.collection.query(
        PartitionKey(
            # Post-migration: uncomment the following line
            # ComposeKey(str(tenant.id), prefix='slots#orgs'),
            ComposeKey(str(tenant.id), prefix='vacancies'),
        )
    )
