"""
Django settings for wilco Django example project.

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-97-n4kmxy!w0ak)vtk(y98jz@!vftl59!0h(8^3i02k!9+qn7z"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get("DJANGO_DEBUG", "False") == "True"

ALLOWED_HOSTS = ["*"] if DEBUG else ["localhost", "127.0.0.1", "0.0.0.0"]


# Application definition

INSTALLED_APPS = [
    "unfold",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "wilco.bridges.django",
    "apps.store",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
    "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 = "config.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "resources" / "templates"],
        "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

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}


# 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 = "en-us"

TIME_ZONE = "UTC"

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 / "resources" / "static"]

MEDIA_URL = "media/"
MEDIA_ROOT = BASE_DIR / "resources" / "media"

# Default primary key field type
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


# Unfold Admin Theme
def _static(path):
    from django.templatetags.static import static

    return lambda request: static(path)


UNFOLD = {
    "SITE_TITLE": "Shop Admin",
    "SITE_HEADER": "Shop Admin",
    "SITE_ICON": _static("apple-touch-icon.png"),
    "SITE_FAVICONS": [
        {"rel": "apple-touch-icon", "sizes": "180x180", "href": _static("apple-touch-icon.png")},
        {"rel": "icon", "type": "image/png", "sizes": "32x32", "href": _static("favicon-32x32.png")},
        {"rel": "icon", "type": "image/png", "sizes": "16x16", "href": _static("favicon-16x16.png")},
    ],
    "STYLES": [_static("css/admin.css")],
}

# Wilco Components
# Load store components from shared common directory with "store" prefix,
# and wilco example components (counter, carousel, etc.) without prefix.
WILCO_COMPONENT_SOURCES = [
    (BASE_DIR.parent / "common" / "components" / "store", "store"),
    (BASE_DIR.parent.parent / "src" / "wilco" / "examples", ""),
]
WILCO_AUTODISCOVER = False

# Pre-built bundles: source directory for wilco build output.
# The WilcoBundleFinder discovers files here and collectstatic copies them
# to STATIC_ROOT/wilco/. Always set so collectstatic can discover bundles
# after they are built; the finder handles the "not built yet" case.
WILCO_BUILD_DIR = str(BASE_DIR / "dist" / "wilco")

# Static files finders (includes WilcoBundleFinder for pre-built bundles)
STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "wilco.bridges.django.finders.WilcoBundleFinder",
]
