version: '3.8'

services:
  # Backend API
  api:
    build:
      context: ./backend
      dockerfile: Dockerfile.dev
    ports:
      - "8080:8080"
    volumes:
      - ./backend:/app
      - /app/tmp
    env_file:
      - ./backend/.env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      zitadel:
        condition: service_healthy
    networks:
      - roadwave-network

  # PostgreSQL + PostGIS
  postgres:
    image: postgis/postgis:16-3.4-alpine
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: roadwave_dev
      POSTGRES_USER: roadwave
      POSTGRES_PASSWORD: dev_password
      POSTGRES_MULTIPLE_DATABASES: zitadel
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./docker/init-multiple-databases.sh:/docker-entrypoint-initdb.d/init-multiple-databases.sh
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U roadwave"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - roadwave-network

  # Redis (caching)
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
    networks:
      - roadwave-network

  # Zitadel (authentication)
  zitadel:
    image: ghcr.io/zitadel/zitadel:latest
    ports:
      - "8081:8080"
    environment:
      ZITADEL_DATABASE_POSTGRES_HOST: postgres
      ZITADEL_DATABASE_POSTGRES_PORT: 5432
      ZITADEL_DATABASE_POSTGRES_DATABASE: zitadel
      ZITADEL_DATABASE_POSTGRES_USER: roadwave
      ZITADEL_DATABASE_POSTGRES_PASSWORD: dev_password
      ZITADEL_DATABASE_POSTGRES_ADMIN_USERNAME: roadwave
      ZITADEL_DATABASE_POSTGRES_ADMIN_PASSWORD: dev_password
      ZITADEL_EXTERNALSECURE: "false"
      ZITADEL_EXTERNALPORT: 8081
      ZITADEL_MASTERKEYPATH: /zitadel-masterkey
    volumes:
      - zitadel_data:/zitadel-masterkey
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/debug/ready"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s
    networks:
      - roadwave-network

  # Adminer (database admin)
  adminer:
    image: adminer:latest
    ports:
      - "8082:8080"
    environment:
      ADMINER_DEFAULT_SERVER: postgres
    depends_on:
      - postgres
    networks:
      - roadwave-network

volumes:
  postgres_data:
  redis_data:
  zitadel_data:

networks:
  roadwave-network:
    driver: bridge
