"""
Django settings for config project.

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

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

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

import os
from pathlib import Path

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-9hwh_v44(3n)61g)tiwkvm1k0h&5c+u=68&z*!$e0ujpd-6^1o'

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

ALLOWED_HOSTS = ["*"]


# Application definition

INSTALLED_APPS = [
    'unfold',  # django-unfold必须在admin之前
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders',
    'django_filters',
    'drf_spectacular',  # Swagger文档生成
    'drf_spectacular_sidecar',
    'shop',
    'community',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    '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',
]

CORS_ALLOW_ALL_ORIGINS = True

CSRF_TRUSTED_ORIGINS = [
    "https://market.quant-speed.com",
    "http://market.quant-speed.com",
]

ROOT_URLCONF = 'config.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'config.wsgi.application'


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

# 数据库配置：默认使用 SQLite，如果有环境变量配置则使用 PostgreSQL
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# 从环境变量获取数据库配置 (Docker 环境会自动注入这些变量)
DB_HOST = os.environ.get('DB_HOST', '6.6.6.66')
if DB_HOST:
    DATABASES['default'] = {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('DB_NAME', 'market'),
        'USER': os.environ.get('DB_USER', 'market'),
        'PASSWORD': os.environ.get('DB_PASSWORD', '123market'),
        'HOST': DB_HOST,
        'PORT': os.environ.get('DB_PORT', '5432'),
    }


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

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_TZ = True


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

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'

# 静态文件配置
STATICFILES_DIRS = [
    BASE_DIR / 'static',
]

# Django REST Framework配置
REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
    'DEFAULT_AUTHENTICATION_CLASSES': [],
    'DEFAULT_PERMISSION_CLASSES': [],
}

# drf-spectacular配置
SPECTACULAR_SETTINGS = {
    'TITLE': '科技公司产品购买API',
    'DESCRIPTION': '科技公司产品购买官网的API文档',
    'VERSION': '1.0.0',
    'SERVE_INCLUDE_SCHEMA': True,
    'SERVE_PERMISSIONS': ['rest_framework.permissions.AllowAny'],
    'COMPONENT_SPLIT_REQUEST': True,
    'SWAGGER_UI_DIST': 'SIDECAR',
    'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
    'REDOC_DIST': 'SIDECAR',
}

from django.urls import reverse_lazy

# django-unfold配置
UNFOLD = {
    "SITE_TITLE": "量迹AI后台",
    "SITE_HEADER": "量迹AI科技硬件/服务商场后台",
    "SITE_URL": "/",
    "COLORS": {
        "primary": {
            "50": "rgb(236 254 255)",
            "100": "rgb(207 250 254)",
            "200": "rgb(165 243 252)",
            "300": "rgb(103 232 249)",
            "400": "rgb(34 211 238)",
            "500": "rgb(6 182 212)",
            "600": "rgb(8 145 178)",
            "700": "rgb(14 116 144)",
            "800": "rgb(21 94 117)",
            "900": "rgb(22 78 99)",
            "950": "rgb(8 51 68)",
        },
    },
    "SIDEBAR": {
        "show_search": True,
        "show_all_applications": False,
        "navigation": [
            {
                "title": "用户管理",
                "separator": True,
                "items": [
                    {
                        "title": "微信用户",
                        "icon": "people",
                        "link": reverse_lazy("admin:shop_wechatuser_changelist"),
                    },
                    {
                        "title": "分销员管理",
                        "icon": "supervisor_account",
                        "link": reverse_lazy("admin:shop_salesperson_changelist"),
                    },
                    {
                        "title": "小程序分销员",
                        "icon": "groups",
                        "link": reverse_lazy("admin:shop_distributor_changelist"),
                    },
                ],
            },
            {
                "title": "商品管理",
                "separator": True,
                "items": [
                    {
                        "title": "硬件配置 (小智参数)",
                        "icon": "hardware",
                        "link": reverse_lazy("admin:shop_esp32config_changelist"),
                    },
                    {
                        "title": "AI服务",
                        "icon": "smart_toy",
                        "link": reverse_lazy("admin:shop_service_changelist"),
                    },
                    {
                        "title": "VC课程",
                        "icon": "school",
                        "link": reverse_lazy("admin:shop_vccourse_changelist"),
                    },
                ],
            },
            {
                "title": "交易管理",
                "separator": True,
                "items": [
                    {
                        "title": "订单列表",
                        "icon": "shopping_cart",
                        "link": reverse_lazy("admin:shop_order_changelist"),
                    },
                    {
                        "title": "服务订单",
                        "icon": "assignment",
                        "link": reverse_lazy("admin:shop_serviceorder_changelist"),
                    },
                    {
                        "title": "提现管理",
                        "icon": "account_balance_wallet",
                        "link": reverse_lazy("admin:shop_withdrawal_changelist"),
                    },
                    {
                        "title": "佣金记录",
                        "icon": "monetization_on",
                        "link": reverse_lazy("admin:shop_commissionlog_changelist"),
                    },
                ],
            },
            {
                "title": "社区与论坛",
                "separator": True,
                "items": [
                    {
                        "title": "活动管理",
                        "icon": "event",
                        "link": reverse_lazy("admin:community_activity_changelist"),
                    },
                    {
                        "title": "活动报名",
                        "icon": "how_to_reg",
                        "link": reverse_lazy("admin:community_activitysignup_changelist"),
                    },
                    {
                        "title": "社区公告",
                        "icon": "campaign",
                        "link": reverse_lazy("admin:community_announcement_changelist"),
                    },
                    {
                        "title": "技术论坛帖子",
                        "icon": "forum",
                        "link": reverse_lazy("admin:community_topic_changelist"),
                    },
                    {
                        "title": "帖子回复",
                        "icon": "chat_bubble",
                        "link": reverse_lazy("admin:community_reply_changelist"),
                    },
                ],
            },
            {
                "title": "系统配置",
                "separator": True,
                "items": [
                    {
                        "title": "微信支付配置",
                        "icon": "payment",
                        "link": reverse_lazy("admin:shop_wechatpayconfig_changelist"),
                    },
                    {
                        "title": "用户认证",
                        "icon": "security",
                        "link": reverse_lazy("admin:auth_user_changelist"),
                    },
                ],
            },
        ],
    },
}

# 重新启用自动补齐斜杠，方便 Admin 使用
# 微信支付回调接口已在 urls.py 中配置 re_path 兼容无斜杠的情况
APPEND_SLASH = True

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO',
    },
}
