from datetime import datetime
from os import getenv
from ast import literal_eval

from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())

# App Variables
APP_NAME = "queue_service"
ENV = getenv("ENV", "PRODUCTION").lower()
BASE_ROUTE = getenv("BASE_ROUTE")
QUEUE_NAME = getenv("QUEUE_NAME")
LOG_LEVEL = getenv("LOG_LEVEL", "DEBUG")
MAX_CONTENT_LENGTH = None
# URLS
USER_SERVICE_BASE_URL = getenv("USER_SERVICE_BASE_URL")
COMMUNICATION_SERVICE_BASE_URL = getenv("COMMUNICATION_SERVICE_BASE_URL")
SCRAPE_SERVICE_BASE_URL = getenv("SCRAPE_SERVICE_BASE_URL")
NOTICE_SERVICE_BASE_URL = getenv("NOTICE_SERVICE_BASE_URL")
QUEUE_SERVICE_BASE_URL = getenv("QUEUE_SERVICE_BASE_URL")
RECOVERY_SERVICE_BASE_URL = getenv("RECOVERY_SERVICE_BASE_URL")
LITIGATION_SERVICE_BASE_URL = getenv("LITIGATION_SERVICE_BASE_URL")
HEROFINCORP_URL = getenv("HEROFINCORP_URL")
UI_SERVICE_BASE_URL = getenv("UI_SERVICE_BASE_URL")
MANHATTAN_SERVICE_BASE_URL = getenv("MANHATTAN_SERVICE_BASE_URL")
ATTEMPTED_DATE = datetime.strptime(getenv("ATTEMPTED_DATE"), "%Y-%m-%d").date()


# DB
POSTGRES = {
    "HOST": getenv("DB_HOST"),
    "PORT": int(getenv("DB_PORT")),
    "USER": getenv("QUEUE_DB_USER"),
    "PASSWORD": getenv("QUEUE_DB_PASSWORD"),
    "NAME": getenv("QUEUE_DB_NAME"),
    "ENABLE_DB_READ_REPLICA": getenv("ENABLE_DB_READ_REPLICA"),
    "READ_REPLICA_DB_HOST": getenv("READ_REPLICA_DB_HOST"),
    "READ_REPLICA_DB_PORT": getenv("DB_PORT"),
}

# CAMPAIGN DB
CAMPAIGN_POSTGRES = {
    "HOST": getenv("DB_HOST"),
    "PORT": int(getenv("DB_PORT")),
    "USER": getenv("CAMPAIGN_DB_USER"),
    "PASSWORD": getenv("CAMPAIGN_DB_PASSWORD"),
    "NAME": getenv("CAMPAIGN_DB_NAME"),
    "ENABLE_DB_READ_REPLICA": getenv("ENABLE_DB_READ_REPLICA"),
    "READ_REPLICA_DB_HOST": getenv("READ_REPLICA_DB_HOST"),
    "READ_REPLICA_DB_PORT": getenv("DB_PORT"),
}
# RECOVERY DB
RECOVERY_POSTGRES = {
    "HOST": getenv("RECOVERY_DB_HOST"),
    "PORT": int(getenv("DB_PORT")),
    "USER": getenv("RECOVERY_DB_USER"),
    "PASSWORD": getenv("RECOVERY_DB_PASSWORD"),
    "NAME": getenv("RECOVERY_DB_NAME"),
    "ENABLE_DB_READ_REPLICA": getenv("ENABLE_RECOVERY_DB_READ_REPLICA"),
    "READ_REPLICA_DB_HOST": getenv("RECOVERY_READ_REPLICA_DB_HOST"),
    "READ_REPLICA_DB_PORT": getenv("DB_PORT"),
}

COMM_POSTGRES = {
    "HOST": getenv("DB_HOST"),
    "PORT": int(getenv("DB_PORT")),
    "USER": getenv("COMMUNICATION_DB_USER"),
    "PASSWORD": getenv("COMMUNICATION_DB_PASSWORD"),
    "NAME": getenv("COMMUNICATION_DB_NAME"),
    "ENABLE_DB_READ_REPLICA": getenv("ENABLE_DB_READ_REPLICA"),
    "READ_REPLICA_DB_HOST": getenv("READ_REPLICA_DB_HOST"),
    "READ_REPLICA_DB_PORT": getenv("DB_PORT"),
}

# Redis
REDIS = {"HOST": getenv("REDIS_HOST"), "PORT": int(getenv("REDIS_PORT"))}


# Celery Variables
CELERY_BROKER_URL = getenv("CELERY_REDIS_URL")
CELERY_RESULT_BACKEND = getenv("CELERY_REDIS_URL")
CELERY_TASK_SERIALIZER = "json"
CELERY_CREATE_MISSING_QUEUES = True
CELERYD_HIJACK_ROOT_LOGGER = True
CELERY_ALWAYS_EAGER = False
CELERY_TASK_RESULT_EXPIRES = 18000
CELERYD_TIME_LIMIT = 86400
CELERY_ACKS_LATE = True
DTMF_RATE_LIMIT = getenv("DTMF_RATE_LIMIT")
DTMF_CHUNK_SIZE = int(getenv("DTMF_CHUNK_SIZE", 100))

# Campaign
CAMPAIGN_MAX_LIMIT = getenv("CAMPAIGN_MAX_LIMIT")
# AWS
# s3
AWS_ACCESS_KEY_ID = getenv("AWS_S3_ACCESS_KEY")
AWS_SECRET_ACCESS_KEY = getenv("AWS_S3_SECRET_KEY")

# Volume Directory
VOLUME_MOUNT_DIRECTORY = getenv("VOLUME_MOUNT_DIRECTORY")
NOTICE_VOLUME_MOUNT_DIRECTORY = getenv("NOTICE_VOLUME_MOUNT_DIRECTORY")

# sendgrid template ids
SENDGRID_API_KEY = getenv("SENDGRID_API_KEY")
SENDGRID_TEMPLATE_ID = getenv("SENDGRID_TEMPLATE_ID")
BATCH_SENDGRID_TEMPLATE_ID = getenv("BATCH_SENDGRID_TEMPLATE_ID")
CAMPAIGN_SENDGRID_TEMPLATE_ID = getenv("CAMPAIGN_SENDGRID_TEMPLATE_ID")
REPORT_SENDGRID_TEMPLATE_ID = getenv("REPORT_SENDGRID_TEMPLATE_ID")
COMPLETION_REPORT_SENDGRID_ID = getenv("COMPLETION_REPORT_SENDGRID_ID")
AI_COMPLETION_REPORT_SENDGRID_ID = getenv("AI_COMPLETION_REPORT_SENDGRID_ID")

# HEROFIN CORP
HEROUSER = getenv("HEROUSER")
HEROPASS = getenv("HEROPASS")


S3_LITIGATION_REPORTS_DIRECTORY = getenv("S3_LITIGATION_REPORTS_DIRECTORY")
LITIGATION_REPORT_BATCH_SIZE = int(getenv("LITIGATION_REPORT_BATCH_SIZE", 1000))
S3_LITIGATION_ORDERS_DIRECTORY = getenv("S3_LITIGATION_ORDERS_DIRECTORY")
S3_BUCKET_NAME = getenv("CG_BUCKET_NAME")
EXPORTS_BUCKET_NAME = getenv("EXPORTS_BUCKET_NAME")
S3_BUCKET_ENDPOINT = getenv("CG_CLOUDFRONT_ENDPOINT")
S3_CLOUDFRONT_BUCKET_ENDPOINT = getenv("CG_CLOUDFRONT_ENDPOINT_1")
DOMAIN_NAME = getenv("DOMAIN_NAME")

SERVICE_NAME = getenv("SERVICE_NAME")
MODULE_NAME = getenv("MODULE_NAME")
CUSTOM_HEADER_RPC_SECRET_KEY = getenv("CUSTOM_HEADER_RPC_SECRET_KEY")
# SMS AUTH KEY
INFOBIP_SMS_AUTH = getenv("INFOBIP_SMS_AUTH")

ELASTIC_APM = {
    # Set the required service name. Allowed characters:
    # a-z, A-Z, 0-9, -, _, and space
    "SERVICE_NAME": getenv("SERVICE_NAME"),
    # Use if APM Server requires a secret token
    "SECRET_TOKEN": getenv("APM_SECRET_TOKEN"),
    # Set the custom APM Server URL (default: http://localhost:8200)
    "SERVER_URL": getenv("APM_SERVER_URL"),
    # Set the service environment
    "ENVIRONMENT": getenv("ENV"),
    "LOG_LEVEL": getenv("LOG_LEVEL", "info").lower(),
    "VERIFY_SERVER_CERT": bool(getenv("APM_VERIFY_SERVER_CERT", "False")),
    "TRANSACTION_IGNORE_URLS": ["*/healthz"],
    "LOCAL_VAR_DICT_MAX_LENGTH": int(getenv("APM_LOCAL_VAR_DICT_MAX_LENGTH", "25")),
    "LOCAL_VAR_MAX_LENGTH": int(getenv("APM_LOCAL_VAR_MAX_LENGTH", "1000")),
    "CAPTURE_BODY": getenv("APM_CAPTURE_BODY", "off"),
    "SPAN_STACK_TRACE_MIN_DURATION": getenv("APM_CAPTURE_BODY", 0),
}

MERGE_NOTICE_CLIENT_ID = getenv("MERGE_NOTICE_CLIENT_ID")
MERGE_NOTICE_TOPIC = getenv("MERGE_NOTICE_TOPIC")
NUMBER_OF_PARTITIONS_FOR_MERGE_NOTICE_TOPIC = int(getenv("NUMBER_OF_PARTITIONS_FOR_MERGE_NOTICE_TOPIC"))

BCC_EMAILS = literal_eval(getenv("BCC_EMAILS", "[]"))

# DSC Variables
TRUECOPY_BASE_URL = getenv("TRUECOPY_BASE_URL")
TRUECOPY_API_KEY = getenv("TRUECOPY_API_KEY")
TRUECOPY_ID = getenv("TRUECOPY_ID")
TRUECOPY_PWD = getenv("TRUECOPY_PWD")
TRUECOPY_ACCESS_ID = getenv("TRUECOPY_ACCESS_ID")
API_TOKEN = getenv("API_TOKEN")
BASE_URL = getenv("BASE_URL")
COUNTRY_WISE_MAPPING = literal_eval(getenv("COUNTRY_WISE_MAPPING", "{}"))
EXPORT_BATCH_SIZE = int(getenv("EXPORT_BATCH_SIZE", "500"))
EXPORT_SERVICE_BASE_URL = getenv("EXPORT_SERVICE_BASE_URL")
AWS_DEFAULT_REGION = getenv("AWS_DEFAULT_REGION")
AWS_S3_ACCESS_KEY = getenv("AWS_S3_ACCESS_KEY")
AWS_S3_SECRET_KEY = getenv("AWS_S3_SECRET_KEY")
EXPORT_EMAIL_TEMPLATE_ID = getenv("EXPORT_EMAIL_TEMPLATE_ID")
STATIC_EMAILS = literal_eval(getenv("STATIC_EMAILS", "['testing.communication@credgenics.com']"))

CELERY_VISIBILITY_SETTINGS_ENABLED = getenv("CELERY_VISIBILITY_SETTINGS_ENABLED", False)
CELERY_VISIBILITY_TIMEOUT = int(getenv("CELERY_VISIBILITY_TIMEOUT", "3600"))

NOTICE_COMPANIES_WITH_PROPERTY_ADDRESS_HANDLING = getenv("NOTICE_COMPANIES_WITH_PROPERTY_ADDRESS_HANDLING", "").split(
    ","
)
LITIGATION_REPORT_DIRECTORY = getenv("LITIGATION_REPORT_DIRECTORY")
API_USER_AUTHENTICATION_TOKEN = getenv("API_USER_AUTHENTICATION_TOKEN")
