import os

from pathlib import Path
from datetime import timedelta
from celery.schedules import crontab

from django.utils.translation import gettext_lazy as _


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


SECRET_KEY = os.environ.get(
    'SECRET_KEY',
    default='m8/o)3$n^03w)mxgvnrxb46__@6qnte9l0dkb7$6%lpbcox+v!'
)

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = int(os.environ.get('DEBUG', default=1))

# 'DJANGO_ALLOWED_HOSTS' should be a single string of hosts with a space between each.
# For example: 'DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]'
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", default='*').split(" ")


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'rest_framework',
    'django_filters',
    'rosetta',
    'djoser',
    'djmoney',
    'djmoney.contrib.exchange',
    'corsheaders',

    'apps.account.apps.AccountConfig',
    'apps.transaction.apps.TransactionConfig',
    'apps.common.apps.CommonConfig',
    'apps.authentication.apps.AuthenticationConfig'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware'
]

ROOT_URLCONF = 'project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages'
            ],
        },
    },
]

WSGI_APPLICATION = 'project.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': os.environ.get('SQL_ENGINE', 'django.db.backends.sqlite3'),
        'NAME': os.environ.get('SQL_DATABASE', os.path.join(BASE_DIR, 'db.sqlite3')),
        'USER': os.environ.get('SQL_USER'),
        'PASSWORD': os.environ.get('SQL_PASSWORD'),
        'HOST': os.environ.get('SQL_HOST'),
        'PORT': os.environ.get('SQL_PORT'),
    }
}

# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

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',
    },
]

AUTH_USER_MODEL = 'authentication.User'

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

LANGUAGE_CODE = 'uk'
LANGUAGES = [
    ('en', _('English')),
    ('uk', _('Ukrainian'))
]

LOCALE_PATHS = (
    BASE_DIR / 'locale',
)

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# ------------------------------------------ #
#                django-money                #
# ------------------------------------------ #
DEFAULT_CURRENCY = 'EUR'

EXCHANGE_BACKEND = 'djmoney.contrib.exchange.backends.FixerBackend'
FIXER_ACCESS_KEY = 'f5a898dbf45d15d8aa6eca7af3f372e1'

# ------------------------------------------ #
#                  Celery                    #
# ------------------------------------------ #
CELERY_BROKER_URL = os.environ.get('REDIS_LOCATION', 'redis://127.0.0.1:6379')
CELERY_RESULT_BACKEND = os.environ.get('REDIS_LOCATION', 'redis://127.0.0.1:6379')
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'


CELERY_BEAT_SCHEDULE = {
    'update_rates': {
        'task': 'apps.user.tasks.update_rates',
        'schedule': crontab(hour="*/1"),
    }
}

# ------------------------------------------ #
#           Django REST Framework            #
# ------------------------------------------ #
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
    'EXCEPTION_HANDLER': 'apps.common.errors.custom_exception_handler',
    'TEST_REQUEST_DEFAULT_FORMAT': 'json',
}

# ------------------------------------------ #
#                   djoser                   #
# ------------------------------------------ #
DJOSER = {
    # Name of a field in User model to be used as login field
    'LOGIN_FIELD': 'email',
    # If True user will be required to click activation
    # link sent in email after:
    # * creating an account
    # * updating their email
    'SEND_ACTIVATION_EMAIL': True,
    'ACTIVATION_URL': '/activate/{uid}/{token}',
    'PASSWORD_RESET_CONFIRM_URL': 'password/reset/confirm/{uid}/{token}',
    'USERNAME_RESET_CONFIRM_URL': 'eamil/reset/confirm/{uid}/{token}',
    # If True, you need to pass re_password to /users/
    # endpoint, to validate password equality.
    'USER_CREATE_PASSWORD_RETYPE': True,
    'PASSWORD_RESET_CONFIRM_RETYPE': True,
    # If True, register or activation endpoint
    # will send confirmation email to user.
    'SEND_CONFIRMATION_EMAIL': True,
    'SERIALIZERS': {
        'user_create': 'apps.user.serializers.UserCreateSerializer'
    }
}

# ------------------------------------------ #
#               Simple JWT                   #
# ------------------------------------------ #
SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
    'ROTATE_REFRESH_TOKENS': True,
    'AUTH_HEADER_TYPES': ('JWT',),
    'USER_ID_FIELD': 'id',
    'USER_ID_CLAIM': 'id',
}

CORS_ALLOW_ALL_ORIGINS = True

[((292, 387), 'os.environ.get', 'os.environ.get', (['"""SECRET_KEY"""'], {'default': '"""m8/o)3$n^03w)mxgvnrxb46__@6qnte9l0dkb7$6%lpbcox+v!"""'}), "('SECRET_KEY', default=\n    'm8/o)3$n^03w)mxgvnrxb46__@6qnte9l0dkb7$6%lpbcox+v!')\n", (306, 387), False, 'import os\n'), ((3704, 3736), 'os.path.join', 'os.path.join', (['BASE_DIR', '"""static"""'], {}), "(BASE_DIR, 'static')\n", (3716, 3736), False, 'import os\n'), ((4255, 4313), 'os.environ.get', 'os.environ.get', (['"""REDIS_LOCATION"""', '"""redis://127.0.0.1:6379"""'], {}), "('REDIS_LOCATION', 'redis://127.0.0.1:6379')\n", (4269, 4313), False, 'import os\n'), ((4338, 4396), 'os.environ.get', 'os.environ.get', (['"""REDIS_LOCATION"""', '"""redis://127.0.0.1:6379"""'], {}), "('REDIS_LOCATION', 'redis://127.0.0.1:6379')\n", (4352, 4396), False, 'import os\n'), ((472, 506), 'os.environ.get', 'os.environ.get', (['"""DEBUG"""'], {'default': '(1)'}), "('DEBUG', default=1)\n", (486, 506), False, 'import os\n'), ((6420, 6440), 'datetime.timedelta', 'timedelta', ([], {'minutes': '(5)'}), '(minutes=5)\n', (6429, 6440), False, 'from datetime import timedelta\n'), ((6472, 6489), 'datetime.timedelta', 'timedelta', ([], {'days': '(1)'}), '(days=1)\n', (6481, 6489), False, 'from datetime import timedelta\n'), ((676, 727), 'os.environ.get', 'os.environ.get', (['"""DJANGO_ALLOWED_HOSTS"""'], {'default': '"""*"""'}), "('DJANGO_ALLOWED_HOSTS', default='*')\n", (690, 727), False, 'import os\n'), ((2375, 2433), 'os.environ.get', 'os.environ.get', (['"""SQL_ENGINE"""', '"""django.db.backends.sqlite3"""'], {}), "('SQL_ENGINE', 'django.db.backends.sqlite3')\n", (2389, 2433), False, 'import os\n'), ((2537, 2563), 'os.environ.get', 'os.environ.get', (['"""SQL_USER"""'], {}), "('SQL_USER')\n", (2551, 2563), False, 'import os\n'), ((2585, 2615), 'os.environ.get', 'os.environ.get', (['"""SQL_PASSWORD"""'], {}), "('SQL_PASSWORD')\n", (2599, 2615), False, 'import os\n'), ((2633, 2659), 'os.environ.get', 'os.environ.get', (['"""SQL_HOST"""'], {}), "('SQL_HOST')\n", (2647, 2659), False, 'import os\n'), ((2677, 2703), 'os.environ.get', 'os.environ.get', (['"""SQL_PORT"""'], {}), "('SQL_PORT')\n", (2691, 2703), False, 'import os\n'), ((3405, 3417), 'django.utils.translation.gettext_lazy', '_', (['"""English"""'], {}), "('English')\n", (3406, 3417), True, 'from django.utils.translation import gettext_lazy as _\n'), ((3431, 3445), 'django.utils.translation.gettext_lazy', '_', (['"""Ukrainian"""'], {}), "('Ukrainian')\n", (3432, 3445), True, 'from django.utils.translation import gettext_lazy as _\n'), ((4625, 4644), 'celery.schedules.crontab', 'crontab', ([], {'hour': '"""*/1"""'}), "(hour='*/1')\n", (4632, 4644), False, 'from celery.schedules import crontab\n'), ((2482, 2518), 'os.path.join', 'os.path.join', (['BASE_DIR', '"""db.sqlite3"""'], {}), "(BASE_DIR, 'db.sqlite3')\n", (2494, 2518), False, 'import os\n'), ((238, 252), 'pathlib.Path', 'Path', (['__file__'], {}), '(__file__)\n', (242, 252), False, 'from pathlib import Path\n')]