from .base import *

DEBUG = False
LIVE_GO_CARDLESS = False
LIVE_MAIL = False
SITE_NAME = os.path.basename(__file__).title()

env_path = os.path.join(BASE_DIR, ".env")
environ.Env.read_env(env_path)

INSTALLED_APPS += ["raven.contrib.django.raven_compat"]

DATABASES = {"default": env.db_url("DATABASE_URL")}

ALLOWED_HOSTS = ["django.iskt.co.uk"]
SECURE_SSL_REDIRECT = False

TEMPLATES[0]["OPTIONS"]["debug"] = DEBUG

STATIC_ROOT = os.path.join(BASE_DIR, "static_files/")
SECRET_KEY = env.str("SECRET_KEY")

BEE_FREE_ID = env.str("BEE_FREE_ID")
BEE_FREE_SECRET = env.str("BEE_FREE_SECRET")

POS_COOKIE = env.str("POS_COOKIE")

if LIVE_MAIL:
    print("Warning - Live mail")
    EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"
    ANYMAIL = env.dict("ANYMAIL")
else:
    EMAIL_BACKEND = "django.core.mail.backends.dummy.EmailBackend"

if LIVE_GO_CARDLESS:
    CARDLESS_ACCESS_TOKEN = env.str("CARDLESS_PRODUCTION_TOKEN")
    CARDLESS_ENVIRONMENT = "live"
    CARDLESS_WEBHOOK_SECRET = env.str("CARDLESS_WEBHOOK_SECRET")
    print("WARNING - LIVE Go Cardless site")
else:
    CARDLESS_ACCESS_TOKEN = env.str("CARDLESS_SANDBOX_TOKEN")
    CARDLESS_ENVIRONMENT = "sandbox"
    CARDLESS_WEBHOOK_SECRET = env.str("CARDLESS_WEBHOOK_SECRET")

# BROKER_URL = env.str('BROKER_URL')

RAVEN_CONFIG = {"dsn": env.str("RAVEN")}
RAVEN_CONFIG = {"dsn": env.str("RAVEN")}

# https://www.webforefront.com/django/setupdjangologging.html
LOGGING = {
    "version": 1,
    "disable_existing_loggers": True,
    "filters": {
        "require_debug_false": {"()": "django.utils.log.RequireDebugFalse"},
        "require_debug_true": {"()": "django.utils.log.RequireDebugTrue"},
    },
    "formatters": {
        "simple": {"format": "[%(asctime)s] %(levelname)s %(message)s", "datefmt": "%Y-%m-%d %H:%M:%S"},
        "verbose": {
            "format": "[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s",
            "datefmt": "%Y-%m-%d %H:%M:%S",
        },
    },
    "handlers": {
        "console": {
            "level": "DEBUG",
            "filters": ["require_debug_true"],
            "class": "logging.StreamHandler",
            "formatter": "simple",
        },
        "production_logfile": {
            "level": "INFO",
            "filters": ["require_debug_false"],
            "class": "logging.handlers.RotatingFileHandler",
            "filename": "./logs/django.log",
            "maxBytes": 1024 * 1024 * 10,  # 10MB
            "backupCount": 5,
            "formatter": "simple",
        },
        "sentry": {
            "level": "ERROR",  # To capture more than ERROR, change to WARNING, INFO, etc.
            "filters": ["require_debug_false"],
            "class": "raven.contrib.django.raven_compat.handlers.SentryHandler",
            "tags": {"custom-tag": SITE_NAME},
        },
    },
    "root": {"level": "DEBUG", "handlers": ["console"]},
    "loggers": {
        "members": {"handlers": ["production_logfile", "sentry"]},
        "django": {"handlers": ["console", "sentry"], "propagate": True},
        # stop sentry logging disallowed host
        "django.security.DisallowedHost": {"handlers": ["console"], "propagate": False},
        "django.request": {  # debug logging of things that break requests
            "handlers": ["production_logfile", "sentry"],
            "level": "DEBUG",
            "propagate": True,
        },
    },
    "py.warnings": {"handlers": ["console"]},
}
