import os
from urllib.parse import urlparse

from bugsink.utils import deduce_allowed_hosts

from bugsink.settings.default import *  # noqa
from bugsink.settings.default import DATABASES

_KIBIBYTE = 1024
_MEBIBYTE = 1024 * _KIBIBYTE
_PORT = os.environ.get("PORT", "8000")


IS_DOCKER = True

DEBUG = os.getenv("DEBUG", "False").lower() in ("true", "1", "yes")
DEBUG_CSRF = "USE_DEBUG" if os.getenv("DEBUG_CSRF") == "USE_DEBUG" else os.getenv("DEBUG_CSRF", "False").lower() in ("true", "1", "yes")

# The security checks on SECRET_KEY are done as part of 'bugsink-manage check --deploy'
SECRET_KEY = os.getenv("SECRET_KEY")


if os.getenv("BEHIND_HTTPS_PROXY", "False").lower() in ("true", "1", "yes"):
    # We hard-tie the ideas of "behind a proxy" and "use https" together: there is no reason to go through the trouble
    # of setting up a proxy if you're not using https. We also hard-code some choices of proxy headers; which means you
    # have to match those on the proxy side (but that's easy enough).
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')  # Note: slightly redundant, Gunicorn also does this.
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True
else:
    # We don't warn about SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE; we can't set them to True because some browsers
    # interpret that as "use https for cookies, even on localhost", and there is no https (BEHIND_HTTPS_PROXY is False).
    # "some browsers": https://forum.djangoproject.com/t/why-are-cookie-secure-settings-defaulted-to-false/1133/4
    SILENCED_SYSTEM_CHECKS += [
        "security.W012",  # SESSION_COOKIE_SECURE
        "security.W016",  # CSRF_COOKIE_SECURE
    ]


# The time-zone here is the default for display purposes (when no project/user configuration is used).
# https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-TIME_ZONE
TIME_ZONE = os.getenv("TIME_ZONE", "UTC")


# Our Docker image is hard-coded to run with snappea in the background; this means we hard-code (as opposed to reading
# the from the env) certain variables: TASK_ALWAYS_EAGER, WORKAHOLIC and DIGEST_IMMEDIATELY.

SNAPPEA = {
    "TASK_ALWAYS_EAGER": False,  # hard-coded, corresponds to Docker setup
    "WORKAHOLIC": True,  # hard-coded, corresponds to Docker setup

    "NUM_WORKERS": int(os.getenv("SNAPPEA_NUM_WORKERS", 2)),
}


if os.getenv("DATABASE_URL"):
    DATABASE_URL = os.getenv("DATABASE_URL")
    parsed = urlparse(DATABASE_URL)

    if parsed.scheme != "mysql":
        raise ValueError("For DATABASE_URL, only mysql is supported.")

    DATABASES['default'] = {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': parsed.path.lstrip('/'),
        "USER": parsed.username,
        "PASSWORD": parsed.password,
        "HOST": parsed.hostname,
        "PORT": parsed.port or "3306",
    }

# else:
#     print("WARNING: DATABASE_URL not set; using default sqlite database, which will not persist to a fresh container.")


if os.getenv("EMAIL_HOST"):
    EMAIL_HOST = os.getenv("EMAIL_HOST")
    EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER")
    EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD")
    EMAIL_PORT = int(os.getenv("EMAIL_PORT", 587))
    EMAIL_USE_TLS = os.getenv("EMAIL_USE_TLS", "True").lower() in ("true", "1", "yes")
else:
    # print("WARNING: EMAIL_HOST not set; email will not be sent")
    EMAIL_BACKEND = "bugsink.email_backends.QuietConsoleEmailBackend"

SERVER_EMAIL = DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", "Bugsink <bugsink@example.org>")

# constants for "create by" (user/team/project) settings
CB_ANYBODY = "CB_ANYBODY"
CB_MEMBERS = "CB_MEMBERS"
CB_ADMINS = "CB_ADMINS"
CB_NOBODY = "CB_NOBODY"


BUGSINK = {
    "DIGEST_IMMEDIATELY": False,  # hard-coded, corresponds to Docker setup

    # The URL where the Bugsink instance is hosted. This is used in the email notifications and to construct DSNs.
    "BASE_URL": os.getenv("BASE_URL", f"http://localhost:{_PORT}"),  # no trailing slash

    # you can customize this as e.g. "My Bugsink" or "Bugsink for My Company"
    "SITE_TITLE": os.getenv("SITE_TITLE", "Bugsink"),

    # Settings for Users, Teams and Projects
    "SINGLE_USER": os.getenv("SINGLE_USER", "False").lower() in ("true", "1", "yes"),

    # who can register new users. default: members, i.e. any user can invite others
    "USER_REGISTRATION": os.getenv("USER_REGISTRATION", CB_MEMBERS),
    "USER_REGISTRATION_VERIFY_EMAIL":
        os.getenv("USER_REGISTRATION_VERIFY_EMAIL", "True").lower() in ("true", "1", "yes"),
    "USER_REGISTRATION_VERIFY_EMAIL_EXPIRY":
        int(os.getenv("USER_REGISTRATION_VERIFY_EMAIL_EXPIRY", 7 * 24 * 60 * 60)),  # 7 days

    # if True, there is only one team, and all projects are in that team
    "SINGLE_TEAM": os.getenv("SINGLE_TEAM", "False").lower() in ("true", "1", "yes"),
    "TEAM_CREATION": os.getenv("TEAM_CREATION", CB_MEMBERS),  # who can create new teams.

    # MAX* below mirror the (current) values for the Sentry Relay.
    "MAX_EVENT_SIZE": int(os.getenv("MAX_EVENT_SIZE", _MEBIBYTE)),
    "MAX_EVENT_COMPRESSED_SIZE": int(os.getenv("MAX_EVENT_COMPRESSED_SIZE", 200 * _KIBIBYTE)),
    "MAX_ENVELOPE_SIZE": int(os.getenv("MAX_ENVELOPE_SIZE", 100 * _MEBIBYTE)),
    "MAX_ENVELOPE_COMPRESSED_SIZE": int(os.getenv("MAX_ENVELOPE_COMPRESSED_SIZE", 20 * _MEBIBYTE)),

    # Bugsink-specific limits:
    # The default values are 1_000 and 5_000 respectively; which corresponds to ~6% and ~2.7% of the total capacity of
    # 50 requests/s (ingestion) on low-grade hardware that I measured, and with 50% of the default value for retention.
    "MAX_EVENTS_PER_PROJECT_PER_5_MINUTES": int(os.getenv("MAX_EVENTS_PER_PROJECT_PER_5_MINUTES", 1_000)),
    "MAX_EVENTS_PER_PROJECT_PER_HOUR": int(os.getenv("MAX_EVENTS_PER_PROJECT_PER_HOUR", 5_000)),

    # Settings that help with debugging and development ("why isn't Bugsink doing what I expect?")
    "VALIDATE_ON_DIGEST": os.getenv("VALIDATE_ON_DIGEST", "none").lower(),  # other legal values are "warn" and "strict"
    "KEEP_ENVELOPES": int(os.getenv("KEEP_ENVELOPES", 0)),  # keep this many in the database; 0 means "don't keep"
}


if os.getenv("ALLOWED_HOSTS"):
    ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS").split(",")
else:
    ALLOWED_HOSTS = deduce_allowed_hosts(BUGSINK["BASE_URL"])
