services:
  frontend:
    image: node:22-alpine
    working_dir: /workspace
    command: sh -lc "npm install && npm run dev -w apps/frontend -- --host 0.0.0.0"
    environment:
      VITE_FRONTEND_URL: http://localhost:3000
      VITE_AUTH_URL: http://localhost:3001
      VITE_API_URL: http://localhost:8080
    ports:
      - "3000:3000"
    volumes:
      - ..:/workspace
    depends_on:
      - auth
      - api

  auth:
    image: node:22-alpine
    working_dir: /workspace
    command: sh -lc "npm install && npm run dev -w apps/backend/auth-service"
    environment:
      FRONTEND_URL: http://localhost:3000
      AUTH_URL: http://localhost:3001
      DATABASE_URL: postgres://productier:productier@postgres:5432/productier?sslmode=disable
      BETTER_AUTH_SECRET: replace-me-with-a-long-random-secret
    ports:
      - "3001:3001"
    volumes:
      - ..:/workspace
    depends_on:
      - postgres

  api:
    image: golang:1.26-alpine
    working_dir: /workspace/apps/backend
    command: sh -lc "go run ./cmd/api"
    environment:
      API_PORT: 8080
      DATABASE_URL: postgres://productier:productier@postgres:5432/productier?sslmode=disable
      AUTH_SERVICE_URL: http://auth:3001
      MAIL_ENCRYPTION_KEY: replace-me-with-a-dedicated-mail-secret
      FILE_STORAGE_PROVIDER: s3
      S3_ENDPOINT: http://rustfs:9000
      S3_REGION: us-east-1
      S3_BUCKET: productier
      S3_ACCESS_KEY: rustfsadmin
      S3_SECRET_KEY: rustfsadmin
      S3_USE_PATH_STYLE: "true"
    ports:
      - "8080:8080"
    volumes:
      - ..:/workspace
    depends_on:
      postgres:
        condition: service_started
      auth:
        condition: service_started
      rustfs:
        condition: service_healthy
      rustfs-init:
        condition: service_completed_successfully

  postgres:
    image: postgres:17-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: productier
      POSTGRES_USER: productier
      POSTGRES_PASSWORD: productier
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data

  rustfs:
    image: rustfs/rustfs@sha256:0725587f6fcca83c1898f321424327d6e6da5e01ea20382905dd258ed5af3be4
    restart: unless-stopped
    environment:
      RUSTFS_VOLUMES: /data/rustfs
      RUSTFS_ADDRESS: 0.0.0.0:9000
      RUSTFS_CONSOLE_ADDRESS: 0.0.0.0:9001
      RUSTFS_CONSOLE_ENABLE: "true"
      RUSTFS_ACCESS_KEY: rustfsadmin
      RUSTFS_SECRET_KEY: rustfsadmin
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - rustfs-data:/data
    healthcheck:
      test: ["CMD", "sh", "-c", "wget -q -O - http://127.0.0.1:9000/health >/dev/null"]
      interval: 15s
      timeout: 5s
      retries: 20

  rustfs-init:
    image: amazon/aws-cli:2.27.42
    restart: "no"
    depends_on:
      rustfs:
        condition: service_healthy
    environment:
      AWS_ACCESS_KEY_ID: rustfsadmin
      AWS_SECRET_ACCESS_KEY: rustfsadmin
      AWS_DEFAULT_REGION: us-east-1
    entrypoint:
      - /bin/sh
      - -lc
      - |
        set -eu
        endpoint="http://rustfs:9000"
        until aws --endpoint-url "$$endpoint" s3api list-buckets >/dev/null 2>&1; do
          sleep 1
        done
        aws --endpoint-url "$$endpoint" s3api head-bucket --bucket productier >/dev/null 2>&1 || \
          aws --endpoint-url "$$endpoint" s3api create-bucket --bucket productier

  greenmail:
    image: greenmail/standalone:2.1.8
    restart: unless-stopped
    environment:
      GREENMAIL_OPTS: >-
        -Dgreenmail.setup.test.all
        -Dgreenmail.users=test1:pwd1@localhost
        -Dgreenmail.users.login=email
        -Dgreenmail.hostname=0.0.0.0
    ports:
      - "3025:3025"
      - "3143:3143"
      - "3465:3465"
      - "3993:3993"
      - "8081:8080"

volumes:
  postgres-data:
  rustfs-data:
