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

from boto3clients import dynamodb_client, s3_client
from config import BUCKET_NAME, ENROLLMENT_TABLE

EXPIRES_IN = 300

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


@router.get('/<id>/download')
def download(id: str):
    params = {
        'Bucket': BUCKET_NAME,
        'Key': f'certs/{id}.pdf',
        'ResponseContentDisposition': f'attachment; filename="{id}.pdf"',
    }

    return {
        'presigned_url': s3_client.generate_presigned_url(
            'get_object',
            Params=params,
            ExpiresIn=EXPIRES_IN,
        )
    }
