"""
Django settings for notino_watcher 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
import environ # pip install django-environ
import os
from celery.schedules import crontab

env = environ.Env()
environ.Env.read_env()

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



# 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 = 'ahiyxj&61mp*%@=o4zr2hf07t_ie2j0djsp()b-j%+$9(yc)zf'

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

ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', os.environ.get("ALLOWED_HOSTS", 'localhost,127.0.0.1,*')).split(',')



ADDRESSEE = os.environ.get('ADDRESSEE', env.str("ADDRESSEE", "") )

# Celery configuration

CELERY_BROKER_URL = os.environ.get('CELERY_BROKER', env.str("CELERY_BROKER", "redis://localhost:6379/0") ) # Declaramos nuestro broker
CELERY_RESULT_BACKEND = os.environ.get('CELERY_BROKER', env.str('CELERY_BROKER', "redis://localhost:6379/0"))
CELERY_ACCEPT_CONTENT = os.environ.get('CELERY_ACCEPT_CONTENT', env.str('CELERY_ACCEPT_CONTENT','application/json')).split(',')
CELERY_TASK_SERIALIZER = os.environ.get('CELERY_TASK_SERIALIZER', env.str("CELERY_TASK_SERIALIZER", "json"))
CELERY_RESULT_SERIALIZER = os.environ.get('CELERY_RESULT_SERIALIZER', env.str("CELERY_RESULT_SERIALIZER", "json"))
CELERY_TIMEZONE = os.environ.get('CELERY_TIMEZONE', env.str("CELERY_TIMEZONE", "Europe/Madrid"))


CELERY_BEAT_SCHEDULE = {
        "sample_task": {
        "task": "fragrance_look_notino_fragrances_task",
        "schedule": crontab(hour=22, minute=55, day_of_week='tuesday'),
        "name": "schedule_fragrance_look_notino_fragrances_task"
    },
}
#"schedule": crontab(minute="*/1"),

# Application definition

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

THIRD_PARTY_APPS = [
    'django_filters',
    'celery',
    'rest_framework'
]

LOCAL_APPS = [
    'fragrance',
    'redis_',
    'demo',
    'core',
    'api'
]

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

MIDDLEWARE = [
    'django.middleware.cache.UpdateCacheMiddleware',
    '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',
    'django.middleware.cache.FetchFromCacheMiddleware',
]

ROOT_URLCONF = 'notino_watcher.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'notino_watcher/templates'),
            os.path.join(BASE_DIR, 'fragrance/templates'),
            os.path.join(BASE_DIR, 'demo/templates'),
        ],
        '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 = 'notino_watcher.wsgi.application'


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


DATABASES2 = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': os.environ.get('DATABASE_NAME', env.str("DATABASE_NAME", 'postgres')),
            'USER': os.environ.get('DATABASE_USER', env.str("DATABASE_USER", 'postgres')),
            'PASSWORD': os.environ.get('DATABASE_PASSWORD', env.str("DATABASE_PASSWORD", 'postgres')),
            'HOST': os.environ.get('DATABASE_HOST', env.str("DATABASE_HOST", 'localhost')),
            'PORT': os.environ.get('DATABASE_PORT', env.str("DATABASE_PORT", '5432')),
        }
}

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": os.environ.get('CACHE_LOCATION', env.str("CACHE_LOCATION", "redis://localhost:6379/1")),
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient"
         },
       "KEY_PREFIX": os.environ.get('CACHE_KEY_PREFIX', env.str("CACHE_KEY_PREFIX", "myredis")),
        }
}

CACHE_TTL = int(os.environ.get('CACHE_TTL', env.str("CACHE_TTL", "900")))
CACHE_KEY_PREFIX = os.environ.get('CACHE_KEY_PREFIX', env.str("CACHE_KEY_PREFIX", "myredis"))



# We need to specify a session engine in the settings. It's important to do so that Redis does not interfere with the Django Admin Panel and the current session.

SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"


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

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


STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]


STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'staticfiles')
