services:
  frontend:
    build:
      context: ./worklenz-frontend
      dockerfile: Dockerfile
    container_name: worklenz_frontend
    ports:
      - "5000:5000"
    depends_on:
      backend:
        condition: service_started
    env_file:
      - ./worklenz-frontend/.env.production
    networks:
      - worklenz

  backend:
    build:
      context: ./worklenz-backend
      dockerfile: Dockerfile
    container_name: worklenz_backend
    ports:
      - "3000:3000"
    depends_on:
      db:
        condition: service_healthy
      minio:
        condition: service_started
    env_file:
      - ./worklenz-backend/.env
    networks:
      - worklenz

  minio:
    image: minio/minio:latest
    container_name: worklenz_minio
    ports:
      - "9000:9000"
      - "9001:9001"
    environment:
      MINIO_ROOT_USER: ${S3_ACCESS_KEY_ID:-minioadmin}
      MINIO_ROOT_PASSWORD: ${S3_SECRET_ACCESS_KEY:-minioadmin}
    volumes:
      - worklenz_minio_data:/data
    command: server /data --console-address ":9001"
    networks:
      - worklenz

  # MinIO setup helper - creates default bucket on startup
  createbuckets:
    image: minio/mc
    container_name: worklenz_createbuckets
    depends_on:
      - minio
    entrypoint: >
      /bin/sh -c '
        echo "Waiting for MinIO to start...";
        sleep 15;
        for i in 1 2 3 4 5; do
          echo "Attempt $i to connect to MinIO...";
          if /usr/bin/mc config host add myminio http://minio:9000 minioadmin minioadmin; then
            echo "Successfully connected to MinIO!";
            /usr/bin/mc mb --ignore-existing myminio/worklenz-bucket;
            /usr/bin/mc policy set public myminio/worklenz-bucket;
            exit 0;
          fi
          echo "Connection failed, retrying in 5 seconds...";
          sleep 5;
        done;
        echo "Failed to connect to MinIO after 5 attempts";
        exit 1;
            '
    networks:
      - worklenz
  db:
    image: postgres:15
    container_name: worklenz_db
    environment:
      POSTGRES_USER: ${DB_USER:-postgres}
      POSTGRES_DB: ${DB_NAME:-worklenz_db}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-password}
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -d ${DB_NAME:-worklenz_db} -U ${DB_USER:-postgres}" ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - worklenz
    volumes:
      - worklenz_postgres_data:/var/lib/postgresql/data
      - type: bind
        source: ./worklenz-backend/database
        target: /docker-entrypoint-initdb.d
        consistency: cached
    command: >
      bash -c ' if command -v apt-get >/dev/null 2>&1; then
        apt-get update && apt-get install -y dos2unix
      elif command -v apk >/dev/null 2>&1; then
        apk add --no-cache dos2unix
      fi && find /docker-entrypoint-initdb.d -type f -name "*.sh" -exec sh -c '\''
        dos2unix "{}" 2>/dev/null || true
        chmod +x "{}"
      '\'' \; && exec docker-entrypoint.sh postgres '

volumes:
  worklenz_postgres_data:
  worklenz_minio_data:


networks:
  worklenz:
