"""
Django settings for GTAnalyzer project.

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

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

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

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'pjnn+jsys$gd(y1ju%1#(6lb*4ko1qvbah=$*nlp)(s)ie*rto'

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

ALLOWED_HOSTS = ['gtanalyzer.herokuapp.com', 'localhost', '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',
    'commons',
    'GAnalyzer',
    'TAnalyzer',
    'rest_framework',
    'gunicorn',
    'corsheaders',
]

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 = 'GTAnalyzer.urls'
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = [
    'http://192.168.0.21:8080',
    'https://gtanalyzer.firebaseapp.com',
    'http://10.153.51.242:8080',
    'http://127.0.0.1:8080',
    'http://localhost:8080'
]
CORS_ORIGIN_REGEX_WHITELIST = [
    'http://192.168.0.21:8080',
    'https://gtanalyzer.firebaseapp.com',
    'http://10.153.51.242:8080',
    'http://127.0.0.1:8080',
    'http://localhost:8080'
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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 = 'GTAnalyzer.wsgi.application'

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny'
    ],
    # 'DEFAULT_THROTTLE_CLASSES': (
    #     'rest_framework.throttling.AnonRateThrottle',
    #     'rest_framework.throttling.UserRateThrottle'
    # ),
    # 'DEFAULT_THROTTLE_RATES': {
    #     'user': '50/day',
    #     'anon': '50/day',
    # }
}

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

GH_API = {
    "GH_TOKEN": os.environ.get("GITHUB_TOKEN"),
    "BASE": "https://api.github.com",
    "V3_HEADER": "application/vnd.github.v3+json",
    "LIST_REPO": "/user/repos?page={}&",
    "CREATE_REPO": "/user/repos",
    "DELETE_REPO": "/repos/{}/{}",  # /repos/:owner/:repo
    "ADD_COLLAB": "/repos/{}/{}/collaborators/{}",  # /repos/:owner/:repo/collaborators/:username
    "BRANCH_PROTECT": "/repos/{}/{}/branches/{}/protection",  # /repos/:owner/:repo/branches/:branch/protection
    "FLIGHT_CHECK": "/user",
    "GET_COMMIT": "/repos/{}/{}/commits?",  # /repos/:owner/:repo/commits
    "GET_SINGLE_COMMIT": "/repos/{}/{}/commits/{}",  # /repos/:owner/:repo/commits/:ref
    "GET_COLLAB": "/repos/{}/{}/collaborators?",  # /repos/:owner/:repo/collaborators
    "GET_PR": "/repos/{}/{}/pulls?state={}",  # /repos/:owner/:repo/pulls
    "GET_BRANCHES": "/repos/{}/{}/branches",  # /repos/:owner/:repo/branches
    "GET_SINGLE_PR": "/repos/{}/{}/pulls/{}",  # /repos/:owner/:repo/pulls/:pull_number
}

TG_API = {
    "TG_TOKEN": os.environ.get("TAIGA_TOKEN"),
    "BASE": "https://api.taiga.io/api/v1",
    "AUTH": "/auth",
    "MEMBERSHIP": "/projects?member={}",
    "CREATE_PROJECT": "/projects",
    "ADD_MEMBERS": "/memberships/bulk_create",
    "PROJECT_BY_SLUG": "/projects/by_slug?slug={}",
    "MILESTONES": "/milestones?project={}",
    "USER_STORIES": "/userstories?project={}",
    "TASKS": "/tasks?project={}",
    "HISTORY_US": "/history/userstory/{}",
    "HISTORY_TASK": "/history/task/{}",
    "EXPORT": "/exporter/{}",
    "MEDIA": "https://media-protected.taiga.io/exports/{}/{}-{}.json"  # /project-id/project-slug-export_id.json
}

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'vb': {
            'format': "%(levelname)s %(asctime)s %(name)s.%(funcName)s:%(lineno)s- %(message)s"
        },
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': os.environ.get('LOG_FILE_LOCATION', '/tmp/gtanalyzer.log'),
            'formatter': 'vb'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        }
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'INFO',
        },
        'django': {
            'handlers': ['file', 'console'],
            'propagate': True,
            'level': 'INFO',
        },
    }
}

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

STATIC_URL = '/static/'
