version: "3.9"
services:
  backend:
    container_name: backend
    build:
      context: . # Build the image from the parent directory
      dockerfile: backend/Dockerfile

    ports:
      - "${PORT}:${PORT}"  # Map container port 8000 to host
    volumes:
      - ./backend:/app/backend  # Map backend directory to /app/backend
      - ./app:/app/app          # Map app directory to /app/app
      - ./config_${ENVIRONMENT}.json:/app/backend/config_${ENVIRONMENT}.json  # Explicitly map config_dev.json
      - ./backend/ssl:/app/backend/ssl  # clearly mount SSL files explicitly into Docker
      - ./uploads:/app/backend/uploads
      - ./uploads:/app/backend/images

    working_dir: /app/backend   # Set working directory to backend/
    command: python main.py     # Command to run main.py
    depends_on: # ⬅️ New addition: wait until postgres is started
      - postgres
    healthcheck:
      test: [ "CMD-SHELL", "curl -k -f https://localhost:${PORT}/openapi.json || exit 1" ]
      interval: 30s
      timeout: 5s
      retries: 5
    environment: # ⬅️ Provide DB info to your backend
      ENVIRONMENT: ${ENVIRONMENT}
      DB_USERNAME: ${DB_USERNAME}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_HOST: postgres
      DB_NAME: ${DB_NAME}
      PORT: ${PORT}

  postgres: # ⬅️ New service (our PostgreSQL database)
    image: postgres:16
    environment:
      POSTGRES_USER: ${DB_USERNAME}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_NAME}
    ports:
      - "5432:5432"
    volumes:
      - ./db_data:/var/lib/postgresql/data


  frontend:
    depends_on:
      backend:
        condition: service_healthy
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        - VITE_OPENAPI_BASE=${VITE_OPENAPI_BASE}
        - VITE_SSL_KEY_PATH=${VITE_SSL_KEY_PATH}
        - VITE_SSL_CERT_PATH=${VITE_SSL_CERT_PATH}
        - NODE_ENV=${NODE_ENV}
    ports:
      - "5173:5173"
    volumes:
      - ./frontend:/app
      - /app/node_modules  # ⬅️ explicit exclusion! ensures Docker-provided modules retain explicitly.
      - ./backend/ssl:/app/backend/ssl
      - ./backend/config_${ENVIRONMENT}.json:/app/backend/config_${ENVIRONMENT}.json  # Dynamically maps config based on environment
    environment:
      VITE_OPENAPI_BASE: ${VITE_OPENAPI_BASE}
      NODE_ENV: ${NODE_ENV}
    command: sh -c "npm run start-${ENVIRONMENT} & ENVIRONMENT=${ENVIRONMENT} npm run watch:openapi"

  logistics_frontend:
    build:
      context: ./logistics
      dockerfile: Dockerfile
      args: # 👈 explicitly pass build args from .env
        - VITE_OPENAPI_BASE=${VITE_OPENAPI_BASE}
        - VITE_SSL_KEY_PATH=${VITE_SSL_KEY_PATH}
        - VITE_SSL_CERT_PATH=${VITE_SSL_CERT_PATH}
        - NODE_ENV=${NODE_ENV}
    ports:
      - "3000:3000"
    depends_on:
      - frontend  # Ensure OpenAPI models are available
    volumes:
      - ./logistics/src:/app/src  # explicitly for active dev (hot reload)
      - ./backend/ssl:/app/backend/ssl  # clearly mount SSL files explicitly into Docker
    environment:
      - VITE_OPENAPI_BASE=${VITE_OPENAPI_BASE}
      - NODE_ENV=${NODE_ENV}
    command: sh -c "npm run start-${ENVIRONMENT}"


volumes: # ⬅️ Persistent storage for PostgreSQL data
  pgdata: