from http import HTTPStatus

from aws_lambda_powertools.event_handler.api_gateway import Router
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair

from api_gateway import JSONResponse
from boto3clients import dynamodb_client
from config import USER_TABLE

router = Router()
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)


@router.get('/<org_id>/admins')
def get_admins(org_id: str):
    return dyn.collection.query(
        # Post-migration: rename `admins` to `ADMIN`
        KeyPair(org_id, 'admins#'),
        limit=100,
    )


@router.post('/<org_id>/admins')
def add(org_id: str):
    return JSONResponse(HTTPStatus.CREATED)


@router.delete('/<org_id>/admins/<user_id>')
def revoke(org_id: str, user_id: str):
    with dyn.transact_writer() as transact:
        transact.delete(
            # Post-migration: rename `admins` to `ADMIN`
            key=KeyPair(org_id, f'admins#{user_id}'),
        )

    return JSONResponse(HTTPStatus.NO_CONTENT)
