from dotenv import load_dotenv
# from http.cookiejar import debug
from pathlib import Path
from datetime import timedelta
import os
from django.templatetags.static import static
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _

# TODO CREATE A ENV WITH THIS NAME
### keep the .env name to .env.local if you want to change this name you should change it in here too when you load this env
load_dotenv(".env.local")

DOMAIN = os.getenv("DOMAIN")
API_DOMAIN = os.getenv("API_DOMAIN")
OPENAI_API_KEY = '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'
# TODO update  telegram bot token
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")

# TODO update email bullshit
EMAIL_BACKEND = os.getenv("EMAIL_BACKEND")
EMAIL_HOST = os.getenv("EMAIL_HOST")
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER")
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD")
DEFAULT_FROM_EMAIL = os.getenv("SECRET_KEY")

SECRET_KEY = os.getenv("SECRET_KEY")
DEBUG = False
# in production lists of allowed hosts and allowed orgins will genrate
# in development every host and orgin will be true
# in prodcution it will use the postgres info you enterd in .env.local
# in development it will use the sqlite 
BASE_DIR = Path(__file__).resolve().parent.parent
if not DEBUG:
    ALLOWED_HOSTS = ['127.0.0.1', 'localhost', DOMAIN, API_DOMAIN]
    CSRF_TRUSTED_ORIGINS = [
        f"https://{DOMAIN}",
        f"http://{DOMAIN}",
    ]
    # CORS_ALLOWED_ORIGINS = [f"https://{API_DOMAIN}", f"http://{API_DOMAIN}",
    #                         f"http://{DOMAIN}", f"https://{DOMAIN}", ]
    CORS_ALLOW_ALL_ORIGINS = True
    # database postgres
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': "hshop",
            'USER': "byeto",
            'PASSWORD': "vuhbyq-cypMu0-sirbon",
            'HOST': 'db',  
            'PORT': 5432,
        }
    }
else:
    CORS_ALLOW_ALL_ORIGINS = True
    # sqlite database
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }



# Application definition

INSTALLED_APPS = [
    # unfold theme
    "unfold",
    "unfold.contrib.filters",
    "unfold.contrib.forms",
    "unfold.contrib.inlines",
    "unfold.contrib.import_export",
    "unfold.contrib.guardian",
    "unfold.contrib.simple_history",
    # django
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # thired party apps
    'corsheaders',
    'rest_framework',
    'drf_spectacular',
    'django_cleanup.apps.CleanupConfig',
    'django_filters',
    'rest_framework_simplejwt',
    'rest_framework_simplejwt.token_blacklist',
    'rest_framework.authtoken',
    # custom apps
    'product',
    'account',
    'ticket',
    'chat',
]

MIDDLEWARE = [
    "whitenoise.middleware.WhiteNoiseMiddleware",
    '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'


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',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

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

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# AUTH_USER_MODEL = 'accounts.User'


REST_FRAMEWORK = {
    # 'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
    'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',

    ],
    'DEFAULT_AUTHENTICATION_CLASSES': (

        'rest_framework_simplejwt.authentication.JWTAuthentication',

    ),
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
    # 'DEFAULT_PERMISSION_CLASSES': [
    #     'rest_framework.permissions.IsAuthenticated',
    # ],
}

SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(days=int(os.getenv("ACCESS_TOKEN_LIFETIME"))),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=int(os.getenv("REFRESH_TOKEN_LIFETIME"))),
    'ROTATE_REFRESH_TOKENS': True,
    'BLACKLIST_AFTER_ROTATION': True,
}

SPECTACULAR_SETTINGS = {
    'TITLE': os.getenv("SITE_TITLE"),
    'DESCRIPTION': os.getenv("SITE_TITLE"),
    'VERSION': '1.0.0',
    'SERVE_INCLUDE_SCHEMA': False,
    'COMPONENT_SPLIT_REQUEST': True
}

UNFOLD = {
    "SITE_TITLE": os.getenv("SITE_TITLE"),
    "SITE_HEADER": os.getenv("SITE_HEADER"),
    "SITE_URL": DOMAIN,
    "SITE_SYMBOL": "shield_person",
    "SITE_FAVICONS": [
        {
            "rel": "icon",
            "sizes": "32x32",
            "type": "image/svg+xml",
            "href": lambda request: static("favicon.svg"),
        },
    ],
    "SHOW_HISTORY": True,
    "SHOW_VIEW_ON_SITE": True,
    "ENVIRONMENT": "core.settings.environment_callback",

    "COLORS": {
        "font": {
            "subtle-light": "107 114 128",
            "subtle-dark": "156 163 175",
            "default-light": "75 85 99",
            "default-dark": "209 213 219",
            "important-light": "17 24 39",
            "important-dark": "243 244 246",
        },
        "primary": {
            "50": "245 250 255",
            "100": "230 243 254",
            "200": "180 218 253",
            "300": "131 193 252",
            "400": "81 168 251",
            "500": "31 144 249",
            "600": "6 118 224",
            "700": "4 92 174",
            "800": "3 66 124",
            "900": "2 39 75",
            "950": "1 13 25"
        },
    },
    "EXTENSIONS": {
        "modeltranslation": {
            "flags": {
                "en": "🇬🇧",
            },
        },
    },

   "SIDEBAR": {
        "show_search": True,
        "show_all_applications": False,
        "navigation": [
            {

                "separator": False,  # Top border
                "collapsible": False,  # Collapsible group of links
                "items": [
                    {
                        "title": _("داشبرد ادمین"),
                        "icon": "dashboard",  
                        "link": reverse_lazy("admin:index"),
                    },
                ],
            },



                          {
                "title": _("پنل فروش محصولات وبسایت"),
                "separator": True,  
                "collapsible": True,  
                "items": [
                    {
                        "title": _("محصولات"),
                        "icon": "redeem",  
                        "link": reverse_lazy("admin:product_productmodel_changelist"),
                    },

                    # esm category model ro lower case bezar inja amir 

                    {
                        "title": _("دسته بندی"),
                        "icon": "category",  
                        "link": reverse_lazy("admin:product_categorymodel_changelist"),
                    },
                    {
                        "title": _("نظرات"),
                        "icon": "chat",  
                        "link": reverse_lazy("admin:product_commentmodel_changelist"),
                    },

                ],
            },

                          {
                "title": _("بخش کاربران و مشتریان"),
                "separator": True,  
                "collapsible": True,  
                "items": [

                    {
                        "title": _("کاربران"),
                        "icon": "person",  
                        "link": reverse_lazy("admin:account_user_changelist"),
                    },

                ],
            },

                {
                "title": _("بخش هوش مصنوعی"),
                "separator": True,  
                "collapsible": True,  
                "items": [

                    {
                        "title": _("چت محصول"),
                        "icon": "chat",  
                        "link": reverse_lazy("admin:chat_productchatmodel_changelist"),
                    },

                ],
            },





        
        ],
    },
}

AUTH_USER_MODEL = 'account.User'
def environment_callback(request):
    return ["Development", "warning"]


def badge_callback(request):
    return 3
