services:
    site-builder:
        build:
            context: ..
            dockerfile: site-builder/Dockerfile
        container_name: astro-site-builder
        ports:
            - "3000:3000"
        environment:
            - S3_ENDPOINT=http://minio:9000
            - S3_REGION=us-east-1
            - S3_ACCESS_KEY=${S3_ACCESS_KEY}
            - S3_SECRET_KEY=${S3_SECRET_KEY}
            - S3_BUCKET_NAME=${S3_BUCKET_NAME:-astro-site}
            - S3_PUBLIC_URL=${S3_PUBLIC_URL:-http://localhost:9000}
        depends_on:
            minio:
                condition: service_healthy
        networks:
            - app-network

    minio:
        image: minio/minio:latest
        container_name: minio
        ports:
            - "9000:9000" # API
            - "9001:9001" # Console
        environment:
            - MINIO_ROOT_USER=${MINIO_ROOT_USER:-minioadmin}
            - MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD:-minioadmin}
        command: server --console-address ":9001" /data
        volumes:
            - minio-data:/data
        healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/ready"]
            interval: 30s
            timeout: 10s
            retries: 3
            start_period: 10s
        networks:
            - app-network

    minio-setup:
        image: minio/mc:latest
        depends_on:
            minio:
                condition: service_healthy
        environment:
            - MINIO_ROOT_USER=${MINIO_ROOT_USER:-minioadmin}
            - MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD:-minioadmin}
            - S3_ACCESS_KEY=${S3_ACCESS_KEY:-access_key}
            - S3_SECRET_KEY=${S3_SECRET_KEY:-secret_key}
            - S3_BUCKET_NAME=${S3_BUCKET_NAME:-astro-site}
        entrypoint: >
            /bin/sh -c '
            sleep 5;
            /usr/bin/mc config host add myminio http://minio:9000 "$${MINIO_ROOT_USER}" "$${MINIO_ROOT_PASSWORD}";
            /usr/bin/mc mb --ignore-existing myminio/"$${S3_BUCKET_NAME}";
            /usr/bin/mc anonymous set download myminio/"$${S3_BUCKET_NAME}";
            /usr/bin/mc admin user add myminio "$${S3_ACCESS_KEY}" "$${S3_SECRET_KEY}";
            /usr/bin/mc admin policy set readwrite myminio user="$${S3_ACCESS_KEY}";
            exit 0;
            '
        networks:
            - app-network

volumes:
    minio-data:

networks:
    app-network:
        driver: bridge
