# -*- coding: utf-8 -*-
import os

from .common import *

#########################################
## GENERIC
#########################################

DEBUG = False

ADMINS = (
    ("Admin", "{{ system_admin_email | default('admin@' ~ ansible_domain) }}"),
)

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': '{{ taiga_db_name }}',
    'USER': '{{ taiga_db_user }}',
    'PASSWORD': '{{ taiga_db_pass }}',
    'HOST': '{{ taiga_db_server }}',
    'PORT': '{{ taiga_db_port }}',
  }
}

SECRET_KEY = "{{ taiga_secret_key }}"

TAIGA_SITES_SCHEME = "{{ taiga_public_url | urlsplit('scheme') }}"
TAIGA_SITES_DOMAIN = "{{ taiga_public_url | urlsplit('hostname') }}"
FORCE_SCRIPT_NAME = "{{ (taiga_public_url | urlsplit('path') == '/') | ternary('', taiga_public_url | urlsplit('path')) }}"

TAIGA_URL = f"{ TAIGA_SITES_SCHEME }://{ TAIGA_SITES_DOMAIN }{ FORCE_SCRIPT_NAME }"
SITES = {
        "api": { "name": "api", "scheme": TAIGA_SITES_SCHEME, "domain": TAIGA_SITES_DOMAIN },
        "front": { "name": "front", "scheme": TAIGA_SITES_SCHEME, "domain": f"{ TAIGA_SITES_DOMAIN }{ FORCE_SCRIPT_NAME }" }
}

# Setting DEFAULT_PROJECT_SLUG_PREFIX to false
# removes the username from project slug
DEFAULT_PROJECT_SLUG_PREFIX = False

#########################################
## MEDIA AND STATIC
#########################################

MEDIA_ROOT = '{{ taiga_root_dir }}/data/media/'
MEDIA_URL = f"{ TAIGA_URL }/media/"
DEFAULT_FILE_STORAGE = "taiga_contrib_protected.storage.ProtectedFileSystemStorage"
THUMBNAIL_DEFAULT_STORAGE = DEFAULT_FILE_STORAGE

# STATIC_ROOT = '/home/taiga/static'
STATIC_URL = f"{ TAIGA_URL }/static/"

#########################################
## EMAIL
#########################################
# https://docs.djangoproject.com/en/3.1/topics/email/
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
CHANGE_NOTIFICATIONS_MIN_INTERVAL = 120  # seconds

DEFAULT_FROM_EMAIL = '{{ taiga_email_from }}'
EMAIL_USE_TLS = {{ taiga_smtp_tls | ternary('True', 'False') }}
EMAIL_USE_SSL = {{ taiga_smtp_ssl | ternary('True', 'False') }}
EMAIL_HOST = '{{ taiga_smtp_server }}'
EMAIL_PORT = {{ taiga_smtp_port }}
{% if taiga_smtp_user is defined and taiga_smtp_pass is defined %}
EMAIL_HOST_USER = '{{ taiga_smtp_user }}'
EMAIL_HOST_PASSWORD = '{{ taiga_smtp_pass }}'
{% endif %}

#########################################
## EVENTS
#########################################
EVENTS_PUSH_BACKEND = "taiga.events.backends.rabbitmq.EventsPushBackend"
EVENTS_PUSH_BACKEND_OPTIONS = {
  "url": "amqp://{{ taiga_amqp_user }}:{{ taiga_amqp_pass }}@{{ taiga_amqp_server }}:{{ taiga_amqp_port }}/{{ taiga_amqp_vhost }}"
}


#########################################
## TAIGA ASYNC
#########################################
CELERY_ENABLED = os.getenv('CELERY_ENABLED', 'True') == 'True'

from kombu import Queue  # noqa

CELERY_BROKER_URL = "amqp://{{ taiga_amqp_user }}:{{ taiga_amqp_pass }}@{{ taiga_amqp_server }}:{{ taiga_amqp_port }}/{{ taiga_amqp_vhost }}"
CELERY_RESULT_BACKEND = None # for a general installation, we don't need to store the results
CELERY_ACCEPT_CONTENT = ['pickle', ]  # Values are 'pickle', 'json', 'msgpack' and 'yaml'
CELERY_TASK_SERIALIZER = "pickle"
CELERY_RESULT_SERIALIZER = "pickle"
CELERY_TIMEZONE = "{{ system_tz | default('Europe/Paris') }}"
CELERY_TASK_DEFAULT_QUEUE = 'tasks'
CELERY_QUEUES = (
  Queue('tasks', routing_key='task.#'),
  Queue('transient', routing_key='transient.#', delivery_mode=1)
)
CELERY_TASK_DEFAULT_EXCHANGE = 'tasks'
CELERY_TASK_DEFAULT_EXCHANGE_TYPE = 'topic'
CELERY_TASK_DEFAULT_ROUTING_KEY = 'task.default'


#########################################
## CONTRIBS
#########################################
# INSTALLED_APPS += [
#     "taiga_contrib_slack",
#     "taiga_contrib_github_auth",
#     "taiga_contrib_gitlab_auth"
# ]
#
# GITHUB_API_CLIENT_ID = "changeme"
# GITHUB_API_CLIENT_SECRET = "changeme"
#
# GITLAB_API_CLIENT_ID = "changeme"
# GITLAB_API_CLIENT_SECRET = "changeme"
# GITLAB_URL = "changeme"


#########################################
## TELEMETRY
#########################################

ENABLE_TELEMETRY = False

#########################################
##  REGISTRATION
#########################################

PUBLIC_REGISTER_ENABLED = {{ (taiga_user_registration or taiga_oidc_auth) | ternary('True', 'False') }}

#########################################
## THROTTLING
#########################################

#REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"] = {
#    "anon-write": "20/min",
#    "user-write": None,
#    "anon-read": None,
#    "user-read": None,
#    "import-mode": None,
#    "import-dump-mode": "1/minute",
#    "create-memberships": None,
#    "login-fail": None,
#    "register-success": None,
#    "user-detail": None,
#    "user-update": None,
#}

# This list should contain:
#  - Taiga users IDs
#  - Valid clients IP addresses (X-Forwarded-For header)
#REST_FRAMEWORK["DEFAULT_THROTTLE_WHITELIST"] = []

# LIMIT ALLOWED DOMAINS FOR REGISTER AND INVITE
# None or [] values in USER_EMAIL_ALLOWED_DOMAINS means allow any domain
#USER_EMAIL_ALLOWED_DOMAINS = None
{% if taiga_user_registration_allowed_domains | length > 0 %}
USER_EMAIL_ALLOWED_DOMAINS=[
{% for domain in taiga_user_registration_allowed_domains %}
  '{{ domain }}'
{% endfor %}
]
{% else %}
USER_EMAIL_ALLOWED_DOMAINS = None
{% endif %}

# PUCLIC OR PRIVATE NUMBER OF PROJECT PER USER
#MAX_PRIVATE_PROJECTS_PER_USER = None # None == no limit
#MAX_PUBLIC_PROJECTS_PER_USER = None # None == no limit
#MAX_MEMBERSHIPS_PRIVATE_PROJECTS = None # None == no limit
#MAX_MEMBERSHIPS_PUBLIC_PROJECTS = None # None == no limit


#########################################
## SITEMAP
#########################################

# If is True /front/sitemap.xml show a valid sitemap of taiga-front client
#FRONT_SITEMAP_ENABLED = False
#FRONT_SITEMAP_CACHE_TIMEOUT = 24*60*60  # In second


#########################################
## FEEDBACK
#########################################

# Note: See config in taiga-front too
FEEDBACK_ENABLED = False
#FEEDBACK_EMAIL = "support@taiga.io"


#########################################
## STATS
#########################################

#STATS_ENABLED = False
#STATS_CACHE_TIMEOUT = 60*60  # In second


#########################################
## IMPORTERS
#########################################

# Configuration for the GitHub importer
# Remember to enable it in the front client too.
#IMPORTERS["github"] = {
#    "active": True,
#    "client_id": "XXXXXX_get_a_valid_client_id_from_github_XXXXXX",
#    "client_secret": "XXXXXX_get_a_valid_client_secret_from_github_XXXXXX"
#}

# Configuration for the Trello importer
# Remember to enable it in the front client too.
#IMPORTERS["trello"] = {
#    "active": True, # Enable or disable the importer
#    "api_key": "XXXXXX_get_a_valid_api_key_from_trello_XXXXXX",
#    "secret_key": "XXXXXX_get_a_valid_secret_key_from_trello_XXXXXX"
#}

# Configuration for the Jira importer
# Remember to enable it in the front client too.
#IMPORTERS["jira"] = {
#    "active": True, # Enable or disable the importer
#    "consumer_key": "XXXXXX_get_a_valid_consumer_key_from_jira_XXXXXX",
#    "cert": "XXXXXX_get_a_valid_cert_from_jira_XXXXXX",
#    "pub_cert": "XXXXXX_get_a_valid_pub_cert_from_jira_XXXXXX"
#}

{% if taiga_oidc_auth %}
INSTALLED_APPS += ["taiga_contrib_openid_auth"]
OPENID_USER_URL = "{{ taiga_oidc_user_url }}"
OPENID_TOKEN_URL = "{{ taiga_oidc_token_url }}"
OPENID_CLIENT_ID = "{{ taiga_oidc_client_id }}"
OPENID_CLIENT_SECRET = "{{ taiga_oidc_client_secret }}"
OPENID_SCOPE = "{{ taiga_oidc_scope }}"
OPENID_ID_FIELD = "{{ taiga_oidc_id_field }}"
OPENID_USERNAME_FIELD = "{{ taiga_oidc_user_field }}"
OPENID_FULLNAME_FIELD = "{{ taiga_oidc_fullname_field }}"
OPENID_EMAIL_FIELD = "{{ taiga_oidc_email_field }}"
{% endif %}
