import logging
import sys

from environs import Env

# TODO: I use a locally patched caprover api for no
sys.path.append(r'/Users/daniel/workspace/iterativ-caprover/Caprover-API')
from caprover_api import caprover_api


logging.basicConfig(level=logging.DEBUG)


env = Env()
env.read_env("./env_secrets/caprover.env")

cap = caprover_api.CaproverAPI(
    dashboard_url=env.str('CAPROVER_DOMAIN'),
    password=env.str('CAPROVER_PASSWORD'),
)

db_pass = env.str('POSTGRES_PASSWORD')
db_user = env.str('POSTGRES_USER')
db_name = env.str('POSTGRES_DB')

cap.deploy_one_click_app(
    one_click_app_name='postgres',
    namespace='vbv-lernwelt',
    # check https://github.com/caprover/one-click-apps/blob/master/public/v4/apps/postgres.yml
    app_variables={
        '$$cap_postgres_version': '14.2',
        '$$cap_pg_user': db_user,
        '$$cap_pg_pass': db_pass,
        '$$cap_pg_db': db_name,
    },
    automated=True
)

cap.create_and_update_app(
    app_name='vbv-lernwelt',
    enable_ssl=True,
    force_ssl=True,
    expose_as_web_app=True,
    image_name='docker.io/iterativ/vbv-lernwelt-django',
    environment_variables={
        # 'DJANGO_SETTINGS_MODULE': 'config.settings.base',
        'IT_DJANGO_SECRET_KEY': env.str('IT_DJANGO_SECRET_KEY'),
        'IT_DJANGO_ADMIN_URL': env.str('IT_DJANGO_ADMIN_URL'),
        'IT_DJANGO_ALLOWED_HOSTS': env.str('IT_DJANGO_ALLOWED_HOSTS'),
        'IT_DJANGO_DEBUG': 'false',
        'IT_SENTRY_DSN': env.str('IT_SENTRY_DSN'),
        'IT_APP_ENVIRONMENT': 'caprover',
        'POSTGRES_HOST': 'srv-captain--vbv-lernwelt-postgres-db',
        'POSTGRES_PORT': 5432,
        'POSTGRES_DB': db_name,
        'POSTGRES_USER': db_user,
        'POSTGRES_PASSWORD': db_pass,
    },
)
