from os import getenv
from dotenv import load_dotenv


# When deploying to Heroku the DATABASE_URL is set to 'postgres://', 
# but since SQLAlquemy version 1.4 the connection uri must start with 'postgresql://'.
# This logic is suppost to fix that.
uri = getenv("DATABASE_URL")
if uri and uri.startswith("postgres://"):
    uri = uri.replace("postgres://", "postgresql://", 1)


class Config:
    load_dotenv()
    TESTING = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    FLASK_RUN_PORT = getenv("FLASK_RUN_PORT")
    DEBUG = getenv("FLASK_DEBUG")
    JWT_SECRET_KEY = getenv("SECRET_KEY")


class DevelopmentConfig(Config):
    SQLALCHEMY_DATABASE_URI = getenv("SQLALCHEMY_DATABASE_URI")
    JSON_SORT_KEYS = False
    DEBUG = True


class ProductionConfig(Config):
    FLASK_ENV = 'production'
    SQLALCHEMY_DATABASE_URI = uri


class TestConfig(Config):
    SQLALCHEMY_DATABASE_URI = "sqlite:///test_db.sqlite3"
    TESTING = True


config_selector = {
    "development": DevelopmentConfig,
    "production": ProductionConfig,
    "test": TestConfig,
}