import logging

from evibes.settings.base import DEBUG, DEBUG_CELERY, DEBUG_DATABASE


class SkipVariableDoesNotExistFilter(logging.Filter):
    def filter(self, record: logging.LogRecord) -> bool:
        if record.exc_info:
            exc_type, _, _ = record.exc_info
            try:
                if exc_type is not None and exc_type.__name__ == "VariableDoesNotExist":
                    return False
            except AttributeError:
                pass
        return "VariableDoesNotExist" not in record.getMessage()


LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "color": {
            "()": "colorlog.ColoredFormatter",
            "format": "%(asctime)s %(log_color)s[%(levelname)s]%(reset)s: %(message)s",
            "datefmt": "%Y-%m-%d %H:%M:%S",
            "log_colors": {
                "DEBUG": "cyan",
                "INFO": "bold_green",
                "WARNING": "bold_yellow",
                "ERROR": "bold_red",
                "CRITICAL": "bold_red,bg_white",
            },
        },
        "plain": {
            "format": "[%(asctime)s] [%(levelname)s] %(name)s: %(message)s",
            "datefmt": "%Y-%m-%d %H:%M:%S",
        },
    },
    "filters": {
        "require_debug_false": {"()": "django.utils.log.RequireDebugFalse"},
        "require_debug_true": {"()": "django.utils.log.RequireDebugTrue"},
        "skip_variable_doesnotexist": {
            "()": "evibes.settings.logconfig.SkipVariableDoesNotExistFilter"
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "color",
            "level": "DEBUG" if DEBUG else "INFO",
        },
        "mail_admins": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "django.utils.log.AdminEmailHandler",
        },
    },
    "root": {
        "handlers": ["console"],
        "level": "DEBUG" if DEBUG else "INFO",
    },
    "loggers": {
        "django": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": True,
        },
        "django.db": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG_DATABASE else "WARNING",
            "propagate": True,
        },
        "django.template": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "ERROR",
            "filters": ["skip_variable_doesnotexist"],
            "propagate": True,
        },
        "uvicorn.access": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": False,
        },
        "uvicorn.error": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "WARNING",
            "propagate": False,
        },
        "celery": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": True,
        },
        "celery.utils": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG_CELERY else "INFO",
            "propagate": True,
        },
        "django_elasticsearch_dsl": {
            "handlers": ["console"],
            "level": "WARNING",
            "propagate": True,
        },
        "elastic_transport.transport": {
            "handlers": ["console"],
            "level": "ERROR",
            "propagate": True,
        },
        "evibes": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": True,
        },
        "blog": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": True,
        },
        "core": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": True,
        },
        "payments": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": True,
        },
        "vibes_auth": {
            "handlers": ["console"],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": True,
        },
    },
}
