from pathlib import Path
from environs import Env

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)

# Get all allowed hosts
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[])

# 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://serva.la")

# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    # 3rd party
    "django_prose_editor",
    "rest_framework",
    "schema_viewer",
    # 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")

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"),
}

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