"""
Django settings for web project.

Generated by 'django-admin startproject' using Django 3.0.4.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
from pathlib import Path

import dj_database_url

# Build paths inside the project like this: BASE_DIR / 'static' / 'css'
BASE_DIR = Path(__file__).parent.parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("SECRET_KEY", "DUMMY_DEV_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
SITE_ID = 1
ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.contrib.gis",
    "django.contrib.sites",
    "apps.reporting",
    "apps.access",
    "rest_framework",
    "django_filters",
    "django_tables2",
    "svg",
    "allauth",
    "allauth.account",
    "allauth.socialaccount",
    "allauth.socialaccount.providers.google",
    "stronghold",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "stronghold.middleware.LoginRequiredMiddleware",
]

ROOT_URLCONF = "config.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "templates",],
        "APP_DIRS": True,
        "OPTIONS": {
            "string_if_invalid": "Invalid template: `%s`",
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "config.context_processors.current_site",
            ],
        },
    },
]

REST_FRAMEWORK = {
    "DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
}
WSGI_APPLICATION = "config.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    "default": dj_database_url.config(
        default="postgis://postgres@db/postgres",
        engine="django.contrib.gis.db.backends.postgis",  # Force postgis
    )
}

# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATICFILES_DIRS = [
    BASE_DIR / "static",
]
STATIC_ROOT = BASE_DIR / "static_root"
STATIC_URL = "/static/"
MEDIA_URL = "/media/"

# Authentication Settings
AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
]

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"
    },
    {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
    {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
    {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
]

LOGIN_REDIRECT_URL = "/"
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
ACCOUNT_ADAPTER = "apps.access.auth.ClosedAccountAdapter"  # Don't allow local signups
SOCIALACCOUNT_ADAPTER = (
    "apps.access.auth.InvitedSocialAccountAdapter"  # Allow invited social signups
)
ACCOUNT_USERNAME_REQUIRED = False  # Use emails as usernames
ACCOUNT_SESSION_REMEMBER = True  # Allow users to stay logged in
ACCOUNT_EMAIL_VERIFICATION = "none"  # Don't require email verification
ACCOUNT_AUTHENTICATION_METHOD = "username_email"  # log in with either username or email
ACCOUNT_DEFAULT_HTTP_PROTOCOL = "https"  # links should use HTTPS
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5  # Timeout after this many failed attempts
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 900  # Duration of timeout (seconds)

STRONGHOLD_PUBLIC_URLS = (r"^/accounts/.+$",)

SOCIALACCOUNT_PROVIDERS = {"google": {"SCOPE": ["profile", "email",],}}

### Local options
# Used to focus geocoding requests
LOCAL_CITY = os.environ.get("LOCAL_CITY", "Oakland, CA")
LOCAL_LONGITUDE = float(os.environ.get("LOCAL_LONGITUDE", "-122.271"))
LOCAL_LATITUDE = float(os.environ.get("LOCAL_LATITUDE", "37.804"))
