services:
  # API Service
  app:
    build:
      context: ..
      dockerfile: apps/api-service/Dockerfile
      target: production
    container_name: banatie-app
    ports:
      - "3000:3000"
    volumes:
      - ../apps/api-service/logs:/app/apps/api-service/logs
      - ../data/results:/app/results
      - ../data/uploads:/app/uploads
    networks:
      - banatie-network
    depends_on:
      postgres:
        condition: service_healthy
      minio:
        condition: service_healthy
    env_file:
      - .env
      - secrets.env
    environment:
      - IS_DOCKER=true
      - NODE_ENV=production
    restart: unless-stopped

  # Landing Page
  landing:
    build:
      context: ..
      dockerfile: apps/landing/Dockerfile
    container_name: banatie-landing
    ports:
      - "3001:3000"
    networks:
      - banatie-network
    depends_on:
      - postgres
    env_file:
      - .env
      - secrets.env
    environment:
      - IS_DOCKER=true
      - NODE_ENV=production
    restart: unless-stopped

  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: banatie-postgres
    ports:
      - "5460:5432"
    volumes:
      - ../data/postgres:/var/lib/postgresql/data
      - ../scripts/init-db.sql:/docker-entrypoint-initdb.d/01-init.sql
    networks:
      - banatie-network
    environment:
      POSTGRES_DB: banatie_db
      POSTGRES_USER: banatie_user
      POSTGRES_PASSWORD: banatie_secure_password
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U banatie_user -d banatie_db"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    restart: unless-stopped

  # MinIO Object Storage - Production Ready with SNMD
  minio:
    image: quay.io/minio/minio:latest
    container_name: banatie-storage
    ports:
      - "9000:9000"  # S3 API
      - "9001:9001"  # Web Console
    volumes:
      # SNMD: 4 drives for full S3 compatibility and erasure coding
      - ../data/storage/drive1:/data1
      - ../data/storage/drive2:/data2
      - ../data/storage/drive3:/data3
      - ../data/storage/drive4:/data4
    networks:
      - banatie-network
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
      MINIO_BROWSER_REDIRECT_URL: http://localhost:9001
      MINIO_SERVER_URL: http://localhost:9000
      MINIO_DOMAIN: localhost
    # CRITICAL: SNMD command for full S3 compatibility
    command: server /data{1...4} --console-address ":9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    restart: unless-stopped

  # MinIO Storage Initialization
  storage-init:
    image: minio/mc:latest
    container_name: banatie-storage-init
    networks:
      - banatie-network
    depends_on:
      minio:
        condition: service_healthy
    entrypoint:
      - /bin/sh
      - -c
      - |
        echo 'Setting up MinIO alias...'
        mc alias set storage http://minio:9000 $${MINIO_ROOT_USER} $${MINIO_ROOT_PASSWORD}

        echo 'Creating main bucket...'
        mc mb --ignore-existing storage/banatie

        echo 'Creating service user...'
        mc admin user add storage banatie_service banatie_service_key_2024

        echo 'Attaching readwrite policy to service user...'
        mc admin policy attach storage readwrite --user=banatie_service

        echo 'Setting up lifecycle policy...'
        cat > /tmp/lifecycle.json <<'LIFECYCLE'
        {
          "Rules": [
            {
              "ID": "temp-cleanup",
              "Status": "Enabled",
              "Filter": {
                "Prefix": "temp/"
              },
              "Expiration": {
                "Days": 7
              }
            }
          ]
        }
        LIFECYCLE
        mc ilm import storage/banatie < /tmp/lifecycle.json

        echo 'Storage initialization completed!'
        echo 'Bucket: banatie'
        echo 'Using presigned URLs for secure access'
        echo 'SNMD mode: Full S3 compatibility enabled'
        exit 0
    restart: "no"

networks:
  banatie-network:
    driver: bridge

volumes:
  postgres-data:
    driver: local
  storage-data:
    driver: local
