# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json
services:
    site:
        build: .
        networks:
            - am-d-model-network
        expose:
            - "3000"
        restart: unless-stopped
        labels:
            - "io.containers.autoupdate=local"

    caddy:
        image: docker.io/library/caddy:latest
        depends_on:
            - site
            - invenio-rdm
        networks:
            - am-d-model-network
        ports:
            - "80:80"
            - "443:443"
        volumes:
            - caddy_data:/data
            - ./caddy:/etc/caddy
        restart: unless-stopped
        labels:
            - "io.containers.autoupdate=registry"
    invenio-rdm:
        image: ghcr.io/front-matter/invenio-rdm-starter:v12.0.13.4
        networks:
            - am-d-model-network
        pull_policy: if_not_present
        volumes:
            - uploaded_data:/opt/invenio/var/instance/data
            - archived_data:/opt/invenio/var/instance/archive
            - ./invenio_assets:/opt/invenio/var/instance/static/custom_assets # Add static assets for theming
            - ./invenio.cfg:/opt/invenio/var/instance/invenio.cfg # Override the config with our custom one
        environment:
            # Production mode
            - INVENIO_ENV=production

            # Flask
            - INVENIO_APP_ALLOWED_HOSTS=${INVENIO_APP_ALLOWED_HOSTS:-['0.0.0.0', 'localhost', '127.0.0.1']}

            # Flask-SQLAlchemy
            - INVENIO_SQLALCHEMY_DATABASE_URI=postgresql+psycopg2://${POSTGRES_USER:-inveniordm}:${POSTGRES_PASSWORD:-inveniordm}@db/${POSTGRES_DB:-inveniordm}

            # Flask-Babel
            - INVENIO_BABEL_DEFAULT_LOCALE=${INVENIO_BABEL_DEFAULT_LOCALE:-en}
            - INVENIO_BABEL_DEFAULT_TIMEZONE=${INVENIO_BABEL_DEFAULT_TIMEZONE:-UTC}

            # Invenio-App
            - INVENIO_CACHE_TYPE=${INVENIO_CACHE_TYPE:-redis}
            - INVENIO_CACHE_REDIS_URL=${INVENIO_CACHE_REDIS_URL:-redis://cache:6379/0}
            - INVENIO_ACCOUNTS_SESSION_REDIS_URL=${INVENIO_ACCOUNTS_SESSION_REDIS_URL:-redis://cache:6379/1}
            - INVENIO_CELERY_RESULT_BACKEND=${INVENIO_CELERY_RESULT_BACKEND:-redis://cache:6379/2}
            - INVENIO_RATELIMIT_STORAGE_URL=${INVENIO_RATELIMIT_STORAGE_URL:-redis://cache:6379/3}
            - INVENIO_COMMUNITIES_IDENTITIES_CACHE_REDIS_URL=${INVENIO_COMMUNITIES_IDENTITIES_CACHE_REDIS_URL:-redis://cache:6379/4}
            - INVENIO_BROKER_URL=${INVENIO_BROKER_URL:-redis://cache:6379/5}
            - INVENIO_CELERY_BROKER_URL=${INVENIO_BROKER_URL:-redis://cache:6379/5}

            - INVENIO_WSGI_PROXIES=4
            - INVENIO_SECRET_KEY=${INVENIO_SECRET_KEY:-changeme}

            # Invenio-I18N
            # TODO: fix tuple parsing
            # - INVENIO_I18N_LANGUAGES=${INVENIO_I18N_LANGUAGES:-'[("de", _("German"))]'}

            # Invenio-Theme
            - INVENIO_THEME_LOGO=${INVENIO_THEME_LOGO:-images/invenio-rdm-white.svg}
            - INVENIO_THEME_SITENAME=${INVENIO_THEME_SITENAME:-InvenioRDM Starter}
            - INVENIO_THEME_FRONTPAGE_TITLE=${INVENIO_THEME_FRONTPAGE_TITLE:-InvenioRDM Starter}
            - INVENIO_THEME_FRONTPAGE_SUBTITLE=${INVENIO_THEME_FRONTPAGE_SUBTITLE:-A starter project for the turn-key research data management repository.}
            - INVENIO_THEME_SHOW_FRONTPAGE_INTRO_SECTION=False

            # Invenio-Records-Resources
            - INVENIO_SITE_UI_URL=${INVENIO_SITE_UI_URL:-https://localhost}
            - INVENIO_SITE_API_URL=${INVENIO_SITE_API_URL:-https://localhost/api}

            # Invenio-RDM-Records
            - INVENIO_DATACITE_ENABLED=${INVENIO_DATACITE_ENABLED:-False}
            - INVENIO_DATACITE_USERNAME=${INVENIO_DATACITE_USERNAME:-}
            - INVENIO_DATACITE_PASSWORD=${INVENIO_DATACITE_PASSWORD:-}
            - INVENIO_DATACITE_PREFIX=${INVENIO_DATACITE_PREFIX:-}
            - INVENIO_DATACITE_TEST_MODE=${INVENIO_DATACITE_TEST_MODE:-True}
            - INVENIO_DATACITE_DATACENTER_SYMBOL=${INVENIO_DATACITE_DATACENTER_SYMBOL:-}

            - INVENIO_RDM_ALLOW_METADATA_ONLY_RECORDS=${INVENIO_RDM_ALLOW_METADATA_ONLY_RECORDS:-True}
            - INVENIO_RDM_ALLOW_RESTRICTED_RECORDS=${INVENIO_RDM_ALLOW_RESTRICTED_RECORDS:-True}
            - INVENIO_RDM_ALLOW_EXTERNAL_DOI_VERSIONING=${INVENIO_RDM_ALLOW_EXTERNAL_DOI_VERSIONING:-True}

            - INVENIO_RDM_CITATION_STYLES_DEFAULT=${INVENIO_RDM_CITATION_STYLES_DEFAULT:-apa}
            - INVENIO_SECURITY_EMAIL_SENDER=${INVENIO_SECURITY_EMAIL_SENDER:-}
            # TODO: fix tuple parsing
            # - INVENIO_RDM_CITATION_STYLES=${INVENIO_RDM_CITATION_STYLES:-"[('apa', 'APA'), ('chicago-note-bibliography', 'Chicago'), ('harvard-cite-them-right', 'Harvard')]"}

            # Invenio-Accounts
            - INVENIO_ACCOUNTS_LOCAL_LOGIN_ENABLED=${INVENIO_ACCOUNTS_LOCAL_LOGIN_ENABLED:-True}
            - INVENIO_GITHUB_APP_CREDENTIALS=${INVENIO_GITHUB_APP_CREDENTIALS:-}

            # OAI-PMH
            - INVENIO_OAISERVER_ID_PREFIX=${OAISERVER_ID_PREFIX:-invenio-rdm}

            # Invenio-Files-REST
            - INVENIO_FILES_REST_STORAGE_FACTORY=invenio_s3.s3fs_storage_factory

            # Invenio-S3
            - INVENIO_S3_ENDPOINT_URL=${INVENIO_S3_ENDPOINT_URL:-}
            - INVENIO_S3_ACCESS_KEY_ID=${INVENIO_S3_ACCESS_KEY_ID:-}
            - INVENIO_S3_SECRET_ACCESS_KEY=${INVENIO_S3_SECRET_ACCESS_KEY:-}
            - INVENIO_S3_BUCKET_NAME=${INVENIO_S3_BUCKET_NAME:-}

            # Invenio-Search
            - INVENIO_SEARCH_HOSTS=${INVENIO_SEARCH_HOSTS:-['search:9200']}
            - INVENIO_SEARCH_INDEX_PREFIX=${INVENIO_SEARCH_INDEX_PREFIX:-invenio-rdm-}

            # Logging
            - INVENIO_LOGGING_CONSOLE_LEVEL=${INVENIO_LOGGING_CONSOLE_LEVEL:-WARNING}

            # Custom invenio settings
            - INVENIO_SECURITY_REGISTERABLE=False # Disable manual user registration
            - INVENIO_MAIL_SUPPRESS_SEND=False # Allow server to send emails
            - INVENIO_RDM_DEFAULT_CITATION_STYLE = "vancouver"
        depends_on:
            search:
                condition: service_started
            cache:
                condition: service_started
            db:
                condition: service_started
    worker:
        command: "celery -A invenio_app.celery worker --beat --events --loglevel=WARNING"
        networks:
            - am-d-model-network
        image: ghcr.io/front-matter/invenio-rdm-starter:v12.0.13.4
        pull_policy: if_not_present
        volumes:
            - uploaded_data:/opt/invenio/var/instance/data
        environment:
            - INVENIO_SQLALCHEMY_DATABASE_URI=postgresql+psycopg2://${POSTGRES_USER:-inveniordm}:${POSTGRES_PASSWORD:-inveniordm}@db/${POSTGRES_DB:-inveniordm}
            - INVENIO_SEARCH_HOSTS=${INVENIO_SEARCH_HOSTS:-['search:9200']}
            - INVENIO_SEARCH_INDEX_PREFIX=${INVENIO_SEARCH_INDEX_PREFIX:-invenio-rdm-}

            - INVENIO_CACHE_TYPE=${INVENIO_CACHE_TYPE:-redis}
            - INVENIO_CACHE_REDIS_URL=${INVENIO_CACHE_REDIS_URL:-redis://cache:6379/0}
            - INVENIO_ACCOUNTS_SESSION_REDIS_URL=${INVENIO_ACCOUNTS_SESSION_REDIS_URL:-redis://cache:6379/1}
            - INVENIO_CELERY_RESULT_BACKEND=${INVENIO_CELERY_RESULT_BACKEND:-redis://cache:6379/2}
            - INVENIO_RATELIMIT_STORAGE_URL=${INVENIO_RATELIMIT_STORAGE_URL:-redis://cache:6379/3}
            - INVENIO_COMMUNITIES_IDENTITIES_CACHE_REDIS_URL=${INVENIO_COMMUNITIES_IDENTITIES_CACHE_REDIS_URL:-redis://cache:6379/4}
            - INVENIO_BROKER_URL=${INVENIO_BROKER_URL:-redis://cache:6379/5}
            - INVENIO_CELERY_BROKER_URL=${INVENIO_BROKER_URL:-redis://cache:6379/5}

            - INVENIO_MAIL_SUPPRESS_SEND=${INVENIO_MAIL_SUPPRESS_SEND:-True}
            - INVENIO_MAIL_SERVER=${INVENIO_MAIL_SERVER:-smtp.gmail.com}
            - INVENIO_MAIL_PORT=${INVENIO_MAIL_PORT:-465}
            - INVENIO_MAIL_USERNAME=${INVENIO_MAIL_USERNAME:-info}
            - INVENIO_MAIL_PASSWORD=${INVENIO_MAIL_PASSWORD:-changeme}
            - INVENIO_MAIL_USE_SSL=${INVENIO_MAIL_USE_SSL:-True}

            # Invenio-Files-REST
            - INVENIO_FILES_REST_STORAGE_FACTORY=${INVENIO_FILES_REST_STORAGE_FACTORY:-invenio_s3.s3fs_storage_factory}

            # Invenio-S3
            - INVENIO_S3_ENDPOINT_URL=${INVENIO_S3_ENDPOINT_URL:-}
            - INVENIO_S3_ACCESS_KEY_ID=${INVENIO_S3_ACCESS_KEY_ID:-}
            - INVENIO_S3_SECRET_ACCESS_KEY=${INVENIO_S3_SECRET_ACCESS_KEY:-}
            - INVENIO_S3_BUCKET_NAME=${INVENIO_S3_BUCKET_NAME:-}
        depends_on:
            search:
                condition: service_started
            cache:
                condition: service_started
            db:
                condition: service_started
    cache:
        image: valkey/valkey:7.2.5-bookworm
        networks:
            - am-d-model-network
        restart: "unless-stopped"
        volumes:
            - "valkey_data:/data"
        ports:
            - "6379:6379"
    db:
        image: postgres:16.4-bookworm
        networks:
            - am-d-model-network
        restart: "unless-stopped"
        environment:
            - POSTGRES_USER=${POSTGRES_USER:-inveniordm}
            - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-inveniordm}
            - POSTGRES_DB=${POSTGRES_DB:-inveniordm}
        volumes:
            - "postgres_data:/var/lib/postgresql/data"
        ports:
            - 5432:5432
    search:
        image: opensearchproject/opensearch:2.12.0
        networks:
            - am-d-model-network
        restart: "unless-stopped"
        environment:
            - bootstrap.memory_lock=true
            - OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m
            - DISABLE_INSTALL_DEMO_CONFIG=true
            - DISABLE_SECURITY_PLUGIN=true
            - discovery.type=single-node
        ulimits:
            memlock:
                soft: -1
                hard: -1
            nofile:
                soft: 65536
                hard: 65536
        mem_limit: 2g
        volumes:
            - opensearch_data:/usr/share/opensearch/data
        ports:
            - "9200:9200"
            - "9600:9600"

networks:
    am-d-model-network:
        name: am-d-model-network

volumes:
    app_data:
    uploaded_data:
    archived_data:
    postgres_data:
    opensearch_data:
    valkey_data:
    caddy_data:
