from collections import OrderedDict
from os import getenv

from django.utils.translation import gettext_lazy as _
from django.utils.translation import gettext_noop

CONSTANCE_BACKEND = "constance.backends.database.DatabaseBackend"
CONSTANCE_SUPERUSER_ONLY = False

CONSTANCE_ADDITIONAL_FIELDS = {
    "json": [
        "django.forms.fields.JSONField",
        {
            "required": False,
            "widget": "core.widgets.JSONTableWidget",
        },
    ],
}

CONSTANCE_CONFIG = OrderedDict(
    [
        ("PROJECT_NAME", (getenv("EVIBES_PROJECT_NAME"), _("Name of the project"))),
        ("FRONTEND_DOMAIN", (getenv("EVIBES_FRONTEND_DOMAIN"), _("Frontend domain name"))),
        ("BASE_DOMAIN", (getenv("EVIBES_BASE_DOMAIN"), _("Base domain name"))),
        ("COMPANY_NAME", (getenv("COMPANY_NAME"), _("Name of the company"))),
        ("COMPANY_ADDRESS", (getenv("COMPANY_ADDRESS"), _("Address of the company"))),
        ("COMPANY_PHONE_NUMBER", (getenv("COMPANY_PHONE_NUMBER"), _("Phone number of the company"))),
        ("EMAIL_BACKEND", ("django.core.mail.backends.smtp.EmailBackend", _("!!!DO NOT CHANGE"))),
        ("EMAIL_HOST", (getenv("EMAIL_HOST", "smtp.404.org"), _("SMTP host"))),
        ("EMAIL_PORT", (int(getenv("EMAIL_PORT", "465")), _("SMTP port"))),
        ("EMAIL_USE_TLS", (bool(int(getenv("EMAIL_USE_TLS", 0))), _("Use TLS (0=No, 1=Yes)"))),
        ("EMAIL_USE_SSL", (bool(int(getenv("EMAIL_USE_SSL", 1))), _("Use SSL (0=No, 1=Yes)"))),
        ("EMAIL_HOST_USER", (getenv("EMAIL_HOST_USER", "no-user@fix.this"), _("SMTP username"))),
        ("EMAIL_HOST_PASSWORD", (getenv("EMAIL_HOST_PASSWORD", "SUPERsecretPASSWORD"), _("SMTP password"))),
        ("EMAIL_FROM", (getenv("EMAIL_FROM", "eVibes"), _("Mail from option"))),
        ("PAYMENT_GATEWAY_URL", (getenv("PAYMENT_GATEWAY_URL", "http://404.org"), _("Payment gateway URL"))),
        ("PAYMENT_GATEWAY_TOKEN", (getenv("PAYMENT_GATEWAY_TOKEN", "example token"), _("Payment gateway token"))),
        ("PAYMENT_GATEWAY_MINIMUM", (getenv("PAYMENT_GATEWAY_MINIMUM", 5.0), _("Payment gateway minimum amount"))),
        ("PAYMENT_GATEWAY_MAXIMUM", (getenv("PAYMENT_GATEWAY_MAXIMUM", 500.0), _("Payment gateway maximum amount"))),
        ("EXCHANGE_RATE_API_KEY", (getenv("EXCHANGE_RATE_API_KEY", "example token"), _("Exchange rate API key"))),
        ("NOMINATIM_URL", (getenv("NOMINATIM_URL", ""), _("OpenStreetMap Nominatim API URL"))),
        ("OPENAI_API_KEY", (getenv("OPENAI_API_KEY", "example key"), _("OpenAI API Key"))),
        ("ABSTRACT_API_KEY", (getenv("ABSTRACT_API_KEY", "example key"), _("Abstract API Key"))),
        ("HTTP_PROXY", (getenv("DJANGO_HTTP_PROXY", "http://username:password@proxy_address:port"), _("HTTP Proxy"))),
        ("DISABLED_COMMERCE", (getenv("DISABLED_COMMERCE", False), _("Disable buy functionality"))),
        ("ADVERTSIMENT", (getenv("EVIBES_ADVERTISIMENT", ""), _("An entity for storing advertisiment data"), "json")),
        ("ANALYTICS", (getenv("EVIBES_ANALYTICS", ""), _("An entity for storing analytics data"), "json")),
        ("SAVE_VENDORS_RESPONSES", (False, _("Save responses from vendors' APIs"))),
        ("TELEGRAM_API_TOKEN", ("", _("Use Telegram-bot functionality"))),
        ("DAYS_TO_STORE_ANON_MSGS", (1, _("How many days we store messages from anonymous users"))),
        ("DAYS_TO_STORE_AUTH_MSGS", (365, _("How many days we store messages from authenticated users"))),
    ]
)

CONSTANCE_CONFIG_FIELDSETS = OrderedDict(
    {
        gettext_noop("General Options"): (
            "PROJECT_NAME",
            "FRONTEND_DOMAIN",
            "BASE_DOMAIN",
            "COMPANY_NAME",
            "COMPANY_ADDRESS",
            "COMPANY_PHONE_NUMBER",
        ),
        gettext_noop("Email Options"): (
            "EMAIL_BACKEND",
            "EMAIL_HOST",
            "EMAIL_PORT",
            "EMAIL_USE_TLS",
            "EMAIL_USE_SSL",
            "EMAIL_HOST_USER",
            "EMAIL_HOST_PASSWORD",
            "EMAIL_FROM",
        ),
        gettext_noop("Payment Gateway Options"): (
            "PAYMENT_GATEWAY_URL",
            "PAYMENT_GATEWAY_TOKEN",
            "EXCHANGE_RATE_API_KEY",
            "PAYMENT_GATEWAY_MINIMUM",
            "PAYMENT_GATEWAY_MAXIMUM",
        ),
        gettext_noop("Features Options"): (
            "TELEGRAM_API_TOKEN",
            "DAYS_TO_STORE_ANON_MSGS",
            "DAYS_TO_STORE_AUTH_MSGS",
            "DISABLED_COMMERCE",
            "NOMINATIM_URL",
            "OPENAI_API_KEY",
            "ABSTRACT_API_KEY",
            "HTTP_PROXY",
        ),
        gettext_noop("SEO Options"): (
            "ADVERTSIMENT",
            "ANALYTICS",
        ),
        gettext_noop("Debugging Options"): ("SAVE_VENDORS_RESPONSES",),
    }
)

EXPOSABLE_KEYS = [
    "PROJECT_NAME",
    "BASE_DOMAIN",
    "COMPANY_NAME",
    "COMPANY_ADDRESS",
    "COMPANY_PHONE_NUMBER",
    "EMAIL_HOST_USER",
    "EMAIL_FROM",
    "PAYMENT_GATEWAY_MINIMUM",
    "PAYMENT_GATEWAY_MAXIMUM",
    "DAYS_TO_STORE_ANON_MSGS",
    "DAYS_TO_STORE_AUTH_MSGS",
    "ADVERTSIMENT",
    "ANALYTICS",
]
