from pathlib import Path
from environs import Env
from import_export.formats.base_formats import CSV

env = Env()
env.read_env()


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "{levelname} {asctime} {module} {process:d} {thread:d} {message}",
            "style": "{",
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        }
    },
    "loggers": {
        "odoo_api": {
            "handlers": ["console"],
            "level": "DEBUG",
            "propagate": True,
        },
    },
}

# 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.str("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DEBUG", default=False)
INTERNAL_IPS = ["127.0.0.1"]

# Get all allowed hosts
original_hosts = env.list("ALLOWED_HOSTS", default=[])
ALLOWED_HOSTS = original_hosts + [
    f"www.{host}" for host in original_hosts if not host.startswith("www.")
]

# Flexible CSRF handling - support both HTTP and HTTPS as needed
HTTP_HOSTS = env.list("HTTP_HOSTS", default=[])
HTTPS_HOSTS = env.list("HTTPS_HOSTS", default=ALLOWED_HOSTS)

CSRF_TRUSTED_ORIGINS = [f"https://{h}" for h in HTTPS_HOSTS] + [
    f"http://{h}" for h in HTTP_HOSTS
]

# Primary website URL
WEBSITE_URL = env.str("WEBSITE_URL", default="https://servala.com")

SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
USE_X_FORWARDED_HOST = True
USE_X_FORWARDED_PORT = True

# Application definition

INSTALLED_APPS = [
    "jazzmin",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.contrib.sitemaps",
    # 3rd party
    "django_prose_editor",
    "rest_framework",
    "schema_viewer",
    "nested_admin",
    "adminsortable2",
    "import_export",
    # local
    "hub.services",
    "hub.broker",
]
if DEBUG:
    INSTALLED_APPS += ["django_browser_reload"]


MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "hub.middleware.PrimaryDomainRedirectMiddleware",
    "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",
]
if DEBUG:
    MIDDLEWARE += [
        "django_browser_reload.middleware.BrowserReloadMiddleware",
    ]


ROOT_URLCONF = "hub.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 = "hub.wsgi.application"


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

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": env.str("DB_FILE", default=str(BASE_DIR / "db.sqlite3")),
        "OPTIONS": {
            "transaction_mode": "IMMEDIATE",
            "timeout": 5,
            "init_command": """
                PRAGMA journal_mode=WAL;
                PRAGMA synchronous=NORMAL;
                PRAGMA mmap_size = 134217728;
                PRAGMA journal_size_limit = 27103364;
                PRAGMA cache_size=2000;
            """,
        },
    }
}


# 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",
    },
]


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

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


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

STATIC_URL = "static/"
STATIC_ROOT = env.path("STATIC_ROOT", default=BASE_DIR / "static")

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

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

MEDIA_URL = "/media/"
MEDIA_ROOT = env.path("MEDIA_ROOT", default=BASE_DIR / "media")


# Email settings using Mailgun EU region
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.eu.mailgun.org"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = env.str("EMAIL_HOST_USER", default="none")
EMAIL_HOST_PASSWORD = env.str("EMAIL_HOST_PASSWORD", default="none")
DEFAULT_FROM_EMAIL = env.str("DEFAULT_FROM_EMAIL", default="noreply@mail.servala.com")


ODOO_CONFIG = {
    "url": env.str("ODOO_URL", default="http://localhost:8069"),
    "db": env.str("ODOO_DB", default="odoo"),
    "username": env.str("ODOO_USERNAME", default="odoo"),
    "password": env.str("ODOO_PASSWORD", default="odoo"),
    "campaign_id": env.str("ODOO_LEAD_CAMPAIGN_ID", default="1"),
    "source_id": env.str("ODOO_LEAD_SOURCE_ID", default="1"),
    "medium_id": env.str("ODOO_LEAD_MEDIUM_ID", default="1"),
    "tag_id": env.str("ODOO_LEAD_TAG_ID", default="1"),
    "mailing_list_id": env.int("ODOO_MAILING_LIST_ID", default=46),
}

BROKER_USERNAME = env.str("BROKER_USERNAME", default="broker")
BROKER_PASSWORD = env.str("BROKER_PASSWORD", default="secret")
BASE_URL = "https://your-domain.com"

SCHEMA_VIEWER = {
    "apps": [
        "services",
    ],
    "exclude": {
        "auth": ["User"],
    },
}

JAZZMIN_SETTINGS = {
    "site_title": "Servala Admin",
    "site_header": "Servala",
    "site_brand": "Servala",
    "site_logo": "img/section-logo.png",
    "welcome_sign": "Welcome to Servala Admin",
    "copyright": "VSHN AG",
    "topmenu_links": [
        {"name": "Home", "url": "admin:index"},
        {
            "name": "Website",
            "url": "https://servala.com",
            "new_window": True,
        },
    ],
    "show_sidebar": True,
    "navigation_expanded": True,
    "hide_apps": ["broker"],
    "order_with_respect_to": ["services", "auth"],
    "changeform_format_overrides": {
        "services.ProgressiveDiscountModel": "single",
        "services.VSHNAppCatPrice": "single",
    },
    "related_modal_active": True,
}

IMPORT_EXPORT_FORMATS = [CSV]
