"""
Django settings for ArticleManagePlus project.

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

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

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

import os
from datetime import timedelta
from pathlib import Path
import sys
import environ

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
sys.path.insert(0, os.path.join(BASE_DIR, "apps"))

env = environ.Env(
    SECRET_KEY=(str, "django-insecure-kifg-tr=&r28uaTmrxz*g$dx66d7+r(&$2y-vut(e$--ths2va"),
    DEFAULT_DATABASE=(str, ""),
    REDIS_URL=(str, ""),
    WECHAT_APPID=(str, ""),
    WECHAT_SECRET=(str, ""),
    WECHAT_REDIRECT_URI=(str, ""),
    CSRF_TRUSTED_ORIGINS=(list, ""),
    MAIN_URL=(str, ""),
    GML_KEY=(str, ""),
    WECHAT_PAY_MCHID=(str, ""),
    WECHAT_PAY_CERT_SERIAL_NO=(str, ""),
    WECHAT_PAY_APIV3_KEY=(str, ""),
    WECHAT_PAY_NOTIFY_URL=(str, ""),
)

# reading .env file
environ.Env.read_env()

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

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

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

ALLOWED_HOSTS = ["*"]


# Application definition

INSTALLED_APPS = [
    "simpleui",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "rest_framework",
    "rest_framework_simplejwt",
    "django_filters",
    "drf_spectacular",
    # "django_q",
    "apps.users",
    "apps.crawlers",
]

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 = "ArticleManagePlus.urls"

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 = "ArticleManagePlus.wsgi.application"
ASGI_APPLICATION = "ArticleManagePlus.asgi.application"


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

DATABASES = {"default": env.db("DEFAULT_DATABASE", "sqlite:///db.sqlite3")}
CACHES = {
    "default": env.cache_url("REDIS_URL"),
}

# Password validation
# https://docs.djangoproject.com/en/5.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",
    },
]

# 自定义User
AUTH_USER_MODEL = "users.Users"

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

LANGUAGE_CODE = "zh-hans"

TIME_ZONE = "Asia/Shanghai"

# 关闭翻译
USE_I18N = True
# 关闭自动转换时区
USE_TZ = False


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

STATIC_URL = "/static/"
if DEBUG:
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]
else:
    STATIC_ROOT = os.path.join(BASE_DIR, "static")

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

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

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# drf
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "rest_framework_simplejwt.authentication.JWTAuthentication",
    ),
    "DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
    "EXCEPTION_HANDLER": "utils.handler.exception_handler",
    "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}

SIMPLE_JWT = {
    "ACCESS_TOKEN_LIFETIME": timedelta(days=7),
}

SIMPLEUI_HOME_PAGE = '/user/dashboard/'
SIMPLEUI_HOME_TITLE = '仪表盘'
SIMPLEUI_HOME_ICON = 'fa fa-tachometer'
# 设置右上角Home图标跳转链接，会以另外一个窗口打开
SIMPLEUI_INDEX = '/user/dashboard/'

SIMPLEUI_LOGO = '/static/docs/logo.png'

SPECTACULAR_SETTINGS = {
    "TITLE": "AiMediaPlus API",
    "DESCRIPTION": "AiMediaPlus API接口文档",
    "VERSION": "1.0.0",
    "SERVE_INCLUDE_SCHEMA": False,
}


WECHAT_APPID = env("WECHAT_APPID")
WECHAT_SECRET = env("WECHAT_SECRET")
WECHAT_REDIRECT_URI = env("WECHAT_REDIRECT_URI")  # 微信授权回调URL

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

CSRF_TRUSTED_ORIGINS = env("CSRF_TRUSTED_ORIGINS")

DATA_UPLOAD_MAX_MEMORY_SIZE = 524288000  # 500MB

main_url = env("MAIN_URL")

GML_KEY = env("GML_KEY")

# 微信支付商户号
WECHAT_PAY_MCHID = env("WECHAT_PAY_MCHID")
# 商户证书序列号
WECHAT_PAY_CERT_SERIAL_NO = env("WECHAT_PAY_CERT_SERIAL_NO")
# API v3密钥
WECHAT_PAY_APIV3_KEY = env("WECHAT_PAY_APIV3_KEY")
# 回调地址
WECHAT_PAY_NOTIFY_URL = env("WECHAT_PAY_NOTIFY_URL")