"""
Django settings for library project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path
from environs import Env
from books.ad_variables import results_return_per_page

env = Env()
env.read_env()

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env.str("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DEBUG", default = False)

ALLOWED_HOSTS = ['127.0.0.1',]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites', #necessary installation for all-auth

    #Third party packages
    'rest_framework', #Rest framework
    'rest_framework.authtoken', # For token authentication
    
    #Third-pary packages for registration and authentication
    'dj_rest_auth',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'dj_rest_auth.registration',

    #Third pary package for password reset, sending to emails
    'django_rest_passwordreset', # To provide password reset endpoints

    #Enabling filter capabilities in browsable api
    'django_filters',
    'django_property_filter',

    #For automated documentation
    'drf_yasg',

    #My apps
    'users', #app for managing user models or accounts
    'books', #app for books
]

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

ROOT_URLCONF = 'library.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 = 'library.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', #Remember to install psycopg2 (pip install psycopg2)
        'NAME': 'library', #This name has to be the exact name as the database you setup in pgadmin 4
        'USER': env.str('USER'),#Default username for postgresql after install postgresql
        'PASSWORD': env.str('PASSWORD'),#Remember the password you used during installation.
        'HOST': '127.0.0.1', #Host server as the name says.
        'PORT': env.int('PORT'), #This was the port used during installation.

    }
}


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


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Europe/London'

DATE_TIME_FORMAT = "%H:%M, %d-%m-%Y"

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/'

AUTH_USER_MODEL = 'users.CustomUser' #Required setting for custom user model to be implemented globally

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [ #Settings used for using session and token authentication
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_PAGINATION_CLASS':
        'books.custompagination.LimitOffsetPaginationWithUpperBound',
        'PAGE_SIZE': results_return_per_page,
    
    'DEFAULT_FILTER_BACKENDS': (
        'django_filters.rest_framework.DjangoFilterBackend',
        'rest_framework.filters.OrderingFilter',
        'rest_framework.filters.SearchFilter',
        ),
}

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' #Sending emails to the console

LOGOUT_ON_PASSWORD_CHANGE = False #Keeps the user logged in after changing password
OLD_PASSWORD_FIELD_ENABLED = True #So when changing password old password has to be entered.

ACCOUNT_EMAIL_REQUIRED = True #user must verify email before accessing api
ACCOUNT_EMAIL_VERIFICATION = "mandatory"

ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = False #Automatically logs users after confirming e-mail.
ACCOUNT_AUTHENTICATION_METHOD = 'username'

ACCOUNT_EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = '/api/v1/login'
ACCOUNT_CONFIRM_EMAIL_ON_GET = True

LOGIN_REDIRECT_URL='/'
LOGIN_URL = 'http://localhost:8000/api/v1/login'

SITE_ID = 1
