from aws_lambda_powertools.event_handler.api_gateway import Router
from layercake.dynamodb import (
    ComposeKey,
    DynamoDBCollection,
    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)
user_collect = DynamoDBCollection(user_layer)
enrollment_collect = DynamoDBCollection(enrollment_layer)


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

    return enrollment_collect.query(
        PartitionKey(
            ComposeKey(str(tenant.id), prefix='vacancies'),
        )
    )
