services:
  db:
    image: postgis/postgis:17-3.5
    container_name: momswap-backend-db
    ports:
      - "7721:5432"
    environment:
      POSTGRES_DB: "${POSTGRES_DB:-momswap}"
      POSTGRES_USER: "${POSTGRES_USER:-momswap}"
      POSTGRES_PASSWORD: "${POSTGRES_PASSWORD:-momswap}"
    volumes:
      - ./var/posrgres:/var/lib/postgresql/data
      - ./etc/pg-init-remote.sh:/docker-entrypoint-initdb.d/99-remote.sh:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-momswap} -d ${POSTGRES_DB:-momswap}"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 10s
    restart: unless-stopped

  minio:
    image: minio/minio:latest
    container_name: momswap-backend-minio
    environment:
      MINIO_ROOT_USER: "${S3_ACCESS_KEY:-momswap}"
      MINIO_ROOT_PASSWORD: "${S3_SECRET_KEY:-momswap-secret}"
    command: server /data --console-address ":9001"
    volumes:
      - ./var/minio:/data
    ports:
      - "8774:9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 10s
    restart: unless-stopped

  minio-init:
    image: minio/mc:latest
    container_name: momswap-backend-minio-init
    environment:
      S3_ACCESS_KEY: "${S3_ACCESS_KEY:-momswap}"
      S3_SECRET_KEY: "${S3_SECRET_KEY:-momswap-secret}"
      S3_BUCKET: "${S3_BUCKET:-momswap-assets}"
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set local http://minio:9000 $$S3_ACCESS_KEY $$S3_SECRET_KEY &&
      mc mb --ignore-existing local/$$S3_BUCKET
      "
    restart: "no"

  api:
    build:
      context: .
      dockerfile: Dockerfile
      target: runtime
    image: momswap-backend:latest
    container_name: momswap-backend-api
    env_file:
      - etc/server-service.env
    environment:
      ADDR: ":8122"
      ADMIN_PUBLIC_KEY: "${ADMIN_PUBLIC_KEY:-}"
      DATABASE_URL: "postgres://${POSTGRES_USER:-momswap}:${POSTGRES_PASSWORD:-momswap}@db:5432/${POSTGRES_DB:-momswap}?sslmode=disable"
      S3_ENDPOINT: "${S3_ENDPOINT:-minio:9000}"
      S3_BUCKET: "${S3_BUCKET:-momswap-assets}"
      S3_REGION: "${S3_REGION:-us-east-1}"
      S3_ACCESS_KEY: "${S3_ACCESS_KEY:-momswap}"
      S3_SECRET_KEY: "${S3_SECRET_KEY:-momswap-secret}"
      S3_USE_PATH_STYLE: "${S3_USE_PATH_STYLE:-true}"
      S3_USE_TLS: "${S3_USE_TLS:-false}"
    volumes:
      - ./etc:/app/etc:ro
      - ./var/logs:/app/var/logs
    depends_on:
      db:
        condition: service_healthy
      minio:
        condition: service_healthy
      minio-init:
        condition: service_completed_successfully
    ports:
      - "8122:8122"
    restart: unless-stopped
    develop:
      watch:
        - action: rebuild
          path: .

  api-dev:
    profiles: ["dev"]
    build:
      context: .
      dockerfile: Dockerfile
      target: dev
    image: momswap-backend:dev
    container_name: momswap-backend-api-dev
    env_file:
      - etc/server-service.env
    environment:
      ADDR: ":8122"
      ADMIN_PUBLIC_KEY: "${ADMIN_PUBLIC_KEY:-}"
      DATABASE_URL: "postgres://${POSTGRES_USER:-momswap}:${POSTGRES_PASSWORD:-momswap}@db:5432/${POSTGRES_DB:-momswap}?sslmode=disable"
      S3_ENDPOINT: "${S3_ENDPOINT:-minio:9000}"
      S3_BUCKET: "${S3_BUCKET:-momswap-assets}"
      S3_REGION: "${S3_REGION:-us-east-1}"
      S3_ACCESS_KEY: "${S3_ACCESS_KEY:-momswap}"
      S3_SECRET_KEY: "${S3_SECRET_KEY:-momswap-secret}"
      S3_USE_PATH_STYLE: "${S3_USE_PATH_STYLE:-true}"
      S3_USE_TLS: "${S3_USE_TLS:-false}"
    volumes:
      - ./etc:/src/etc:ro
      - ./var/logs:/src/var/logs
    depends_on:
      db:
        condition: service_healthy
      minio:
        condition: service_healthy
      minio-init:
        condition: service_completed_successfully
    ports:
      - "8122:8122"
    restart: unless-stopped
    develop:
      watch:
        - action: sync+restart
          path: ./web
          target: /src/web
        - action: sync+restart
          path: ./internal
          target: /src/internal
        - action: sync+restart
          path: ./cmd
          target: /src/cmd
        - action: rebuild
          path: ./go.mod
        - action: rebuild
          path: ./Dockerfile

  test:
    build:
      context: .
      dockerfile: Dockerfile
      target: test
    image: momswap-backend:test
    volumes:
      - .:/src
    working_dir: /src
    user: root
    profiles:
      - test

