# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json

# ^ this line provides nicer autocomplete in some editors

services:
    # the core platform service
    platform:
        depends_on:
            db:
                condition: service_started
            platform-jobs:
                condition: service_started
            platform-migrations:
                condition: service_completed_successfully
            minio-init:
                condition: service_completed_successfully
        platform: linux/amd64
        image: ghcr.io/pubpub/platform:latest
        env_file: .env
        environment:
            DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
        ports:
            - "3000:3000"
        networks:
            - app-network

    # platfrom jobs service
    # takes care of longer running tasks like scheduling actions
    platform-jobs:
        depends_on:
            platform-migrations:
                condition: service_completed_successfully
        platform: linux/amd64
        image: ghcr.io/pubpub/platform-jobs:latest
        env_file: .env
        environment:
            DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
            # change this if you change the platform port
            PUBPUB_URL: http://platform:3000
        networks:
            - app-network

    platform-migrations:
        platform: linux/amd64
        depends_on:
            db:
                condition: service_started
        image: ghcr.io/pubpub/platform-migrations:latest
        env_file: .env
        environment:
            DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
        command: ["pnpm", "--filter", "core", "migrate-docker"]
        networks:
            - app-network

    # cache
    cache:
        image: valkey/valkey:8-alpine
        restart: always
        env_file: .env
        networks:
            - app-network

    # database
    # can be removed if you manually set the DATABASE_URL environment variable in .env
    # to another postgres database you have access to
    db:
        image: postgres:15
        restart: always
        env_file: .env
        volumes:
            - postgres-data:/var/lib/postgresql/data
        networks:
            - app-network
        ports:
            - "${POSTGRES_PORT}:${POSTGRES_PORT}" # you can choose another port if you want, just make sure the first one matches the POSTGRES_PORT in .env

    # recommended reverse proxy solution
    # can be removed if you manually set up a reverse proxy like nginx instead
    # useful if you want your assets, platform, and site to be on the same domain
    # but with different paths
    # caddy:
    #     image: caddy:latest
    #     depends_on:
    #         - platform
    #         - platform-jobs
    #         - minio
    #     env_file:
    #         - path: .env
    #           required: true
    #     ports:
    #         - "443:443"
    #     volumes:
    #         - ./caddy:/etc/caddy
    #         - caddy-data:/data
    #         - caddy-config:/config
    #     networks:
    #         - app-network

    # assets storage
    # can be removed if you manually set up a storage service like s3 or minio instead
    minio:
        image: minio/minio:latest
        env_file: .env
        healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/ready"]
            interval: 1m30s
            timeout: 30s
            retries: 5
            start_period: 60s
            start_interval: 1s
        restart: unless-stopped
        command: server --console-address ":9001" /data
        ports:
            - "9000:9000" # API
            - "9001:9001" # Console
        # volumes:
        #     - ./minio:/data
        networks:
            - app-network

    # initialize minio
    minio-init:
        depends_on:
            minio:
                condition: service_healthy
        image: minio/mc:latest
        env_file: .env
        entrypoint: ["/bin/sh", "/minio-init.sh"]
        volumes:
            - ./minio-init.sh:/minio-init.sh
        networks:
            - app-network

    site-builder:
        image: ghcr.io/pubpub/platform-site-builder:latest
        env_file: .env
        networks:
            - app-network
        ports:
            - "4000:4000"
        depends_on:
            - platform
            - platform-jobs
            - minio

volumes:
    caddy-data:
    caddy-config:
    postgres-data:

networks:
    app-network:
