"Function to standardize the connection to DynamoDB"
import os

import boto3
# boto3 configurations
from botocore.config import Config

config = Config(retries={"max_attempts": 5, "mode": "adaptive"})


class DynamoConnector:
    "Class to standardize the connection to DynamoDB"

    def __init__(self, region_name="us-west-1"):
        "Constructor to initialize the connection to DynamoDB"
        is_local = os.environ.get("AWS_SAM_LOCAL") == "true"
        # self.dynamodb = boto3.resource("dynamodb", region_name=region_name, config = config)
        if is_local:
            # self.dynamodb = boto3.resource("dynamodb", endpoint_url="http://localhost:8000", config=config)
            self.dynamodb = boto3.resource(
                "dynamodb", region_name=region_name, config=config
            )
        else:
            self.dynamodb = boto3.resource(
                "dynamodb", region_name=region_name, config=config
            )

    def get_table(self, table_name):
        "Method to get the table from DynamoDB"
        return self.dynamodb.Table(table_name)


class SecretManager:
    def __init__(self, region_name="us-west-1"):
        self.secret_manager = boto3.client(
            "secretsmanager", region_name=region_name, config=config
        )

    def get_secret(self, secret_name, region_name="us-west-1"):
        return self.secret_manager.get_secret_value(SecretId=secret_name)[
            "SecretString"
        ]


secret_manager = SecretManager()
