import os
import boto3
import json

from app.shared.clients.secret_manager import SecretManager

def _map_to_auth_response(principal, resource, effect):
    statement = {
        "Action": "execute-api:Invoke",
        "Effect": effect,
        "Resource": resource
    }

    policy_document = {
        "Version": "2012-10-17",
        "Statement": [statement]
    }

    response = {
        "principalId": principal,
        "policyDocument": policy_document
    }

    print('Response: ', response)
    return response

def authorize(resource: str, principal: str, basic_string: str) -> str:

    expected_basic_string = SecretManager().secret(secret_name='API_AUTH_KEY', key_name='BASIC_AUTH_KEY')
    print(f'AUTH KEY in Secrets Manager: {expected_basic_string}')
    print(f'AUTH KEY in Request: {basic_string}')

    if basic_string == expected_basic_string:
        effect = 'Allow'
    else:
        effect = 'Deny'

    return _map_to_auth_response(principal, resource, effect)