import os
from datetime import timedelta

try:
    from local_config import LocalConfig
except ImportError:
    print("   \033[92mWARNING: Local config not found\033[0m")

    # The LocalConfig class serves a dual purpose and overrides settings in
    # both V5Config and some fields of FlaskApplicationSettings. The first has
    # defaults (DefaultConfig), but not the second, so we provide them here.
    class LocalConfig():
        FLASK_DEBUG = False
        DB_NAME = "pcv5"
        SECRET_KEY = "a-random-secret-key"

#---
# Flask configuration
#---

class FlaskApplicationSettings(object):
    """
    This object specifies the settings for the Flask application. All the keys
    and values are predefined by Flask.

    See: https://flask.palletsprojects.com/en/2.1.x/config/
    """

    DEBUG = os.environ.get("FLASK_DEBUG") or LocalConfig.FLASK_DEBUG

    SECRET_KEY = os.environ.get('SECRET_KEY') or LocalConfig.SECRET_KEY
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
        'postgresql+psycopg2://' + os.environ.get('USER') + ':@/' \
        + LocalConfig.DB_NAME
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    MAIL_DEFAULT_SENDER = "noreply@v5.planet-casio.com"
    MAIL_SUPPRESS_SEND = None

    # Only send cookies over HTTPS connections (use only if HTTPS is enabled)
    SESSION_COOKIE_SECURE = True
    # Only send cookies in requests, do not expose them to Javascript
    SESSION_COOKIE_HTTPONLY = True
    # Do not attach cookies to cross-origin requests
    SESSION_COOKIE_SAMESITE = "Lax"

#---
# v5 configuration
#---

class DefaultConfig(object):
    # Domain
    DOMAIN = "v5.planet-casio.com"
    # Database name
    DB_NAME = "pcv5"
    # LDAP usage
    USE_LDAP = False
    # LDAP configuration
    LDAP_PASSWORD = "openldap"
    LDAP_ROOT = "o=planet-casio"
    LDAP_ENV = "o=prod"
    # Secret key used to authenticate tokens. **USE YOURS!**
    SECRET_KEY = "a-random-secret-key"
    # Uploaded data folder
    DATA_FOLDER = '/var/www/uploads'
    # Enable guest post
    ENABLE_GUEST_POST = True
    # Disable email confimation
    ENABLE_EMAIL_CONFIRMATION = True
    # Send emails
    SEND_MAILS = True
    # GLaDOS configuration
    GLADOS_HOST = "127.0.0.1"
    GLADOS_PORT = 5555
    # Time before trigerring the necropost alert
    NECROPOST_LIMIT = timedelta(days=31)
    # Acceptable page loading time; longer generation is reported to devs. This
    # is computed in the page header, so it doesn't account for most of the
    # template generation.
    SLOW_REQUEST_THRESHOLD = 0.400 # s
    # Whether to enable flask-debug-toolbar
    ENABLE_FLASK_DEBUG_TOOLBAR = False


class V5Config(LocalConfig, DefaultConfig):
    """
    This object holds the settings for the v5 code. Each parameter has the
    value specified in LocalConfig (if any), and defaults to the value set in
    DefaultConfig.
    """
    pass
