"""
Django settings for core project.

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

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

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

from pathlib import Path
import os
from decouple import config
from firebase_admin import credentials, initialize_app, firestore
from google.oauth2 import service_account
from googleapiclient.discovery import build
import logging
from corsheaders.defaults import default_headers


# 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/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-ewy-ozt51a!tuk63y5l%5o-t0d!0^_kqy%j7@$p+mag#durbb_'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ["*"]

CORS_ALLOWED_ORIGINS = ["http://localhost:3000", "http://192.168.1.65:8081"]
CORS_ALLOW_HEADERS = list(default_headers) + [
    'Role',
]

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'authentication',
    'events',
    'payment',
    'photobook',
    'media',
    'rest_framework',
]

"""FIREBASE SETUP"""
# cred = credentials.Certificate({
#     "type" : config('type'),
#     "project_id" : config('project_id'),
#     "private_key_id" : config('private_key_id'),
#     "private_key" : config('private_key'),
#     "client_email" : config('client_email'),
#     "client_id" : config('client_id'),
#     "auth_uri" : config('auth_uri'),
#     "token_uri" : config('token_uri'),
#     "auth_provider_x509_cert_url" : config('auth_provider_x509_cert_url'),
#     "client_x509_cert_url" : config('client_x509_cert_url')
# })
cred = credentials.Certificate("./wdn-platform-firebase-adminsdk.json")
default_app = initialize_app(cred)
FIRESTORE_DB = firestore.client()

"""GOOGLE DRIVE SETUP"""
drive_credentials = service_account.Credentials.from_service_account_file('./google-drive-wdn-platform-7b6dcfea030c.json',
                                                                          scopes=["https://www.googleapis.com/auth/drive",
                                                                                  "https://www.googleapis.com/auth/drive.file",
                                                                                  "https://www.googleapis.com/auth/drive.appdata",
                                                                                  "https://www.googleapis.com/auth/drive.scripts",
                                                                                  "https://www.googleapis.com/auth/drive.metadata"])
DRIVE_SERVICE = build('drive', 'v3', credentials=drive_credentials)
CLIENTS_FOLDER_CONTENT_ID = config(
    "CLIENTS_FOLDER_CONTENT_ID", default="16pmNS8BRmPrdQnf32EOtJv83qxTKFwhq")
PROFILEPICTURES_FOLDER_CONTENT_ID = config(
    "PROFILEPICTURES_FOLDER_CONTENT_ID", default="1_2YvZxZseOraDs0nh1saB3LH5j7ckFJl")
EVENTBOOKINGS_FOLDER_CONTENT_ID = config(
    "EVENTBOOKINGS_FOLDER_CONTENT_ID", default="1nq4n-ickrzFRAn8v0TLolUANd06y5Zw1")

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',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

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


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

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


# Password validation
# https://docs.djangoproject.com/en/4.2/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/4.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


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

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

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# Email configurations
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = config('EMAIL_HOST')
EMAIL_PORT = config('EMAIL_PORT')
EMAIL_USE_TLS = config('EMAIL_USE_TLS')
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')

# External APIs
SMS_API_URL = config('SMS_API_URL')

# log configuration
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'class': 'logging.FileHandler',
            'filename': 'app.log',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
        },
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
    }
}

logging.basicConfig(level=logging.INFO)
