services:
  # ==========================================
  # Core API Service
  # ==========================================
  api:
    build:
      context: ../..
      dockerfile: infra/docker/Dockerfile
    container_name: tjwater_api
    restart: always
    ports:
      - "${API_PORT}:8000"
    volumes:
      - ../../app:/app/app
      - ../../resources:/app/resources
    environment:
      - PYTHONPATH=/app
      - REDIS_HOST=redis
      - REDIS_PORT=${REDIS_PORT}
      - REDIS_PASSWORD=${REDIS_PASSWORD}
      # Add other DB connections here as needed by your app
    depends_on:
      - redis
      - timescaledb
      - postgis

  # ==========================================
  # Infrastructure Services
  # ==========================================

  # --- Redis ---
  redis:
    image: redis:latest
    container_name: redis
    restart: always
    command: redis-server --requirepass ${REDIS_PASSWORD}
    ports:
      - "${REDIS_PORT}:6379"
    volumes:
      - ./redis/data:/data

  # --- InfluxDB ---
  influxdb:
    image: influxdb:2.7
    container_name: influxdb
    restart: always
    environment:
      DOCKER_INFLUXDB_INIT_MODE: setup
      DOCKER_INFLUXDB_INIT_USERNAME: ${INFLUXDB_USER}
      DOCKER_INFLUXDB_INIT_PASSWORD: ${INFLUXDB_PASSWORD}
      DOCKER_INFLUXDB_INIT_ORG: ${INFLUXDB_ORG}
      DOCKER_INFLUXDB_INIT_BUCKET: ${INFLUXDB_BUCKET}
      DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: ${INFLUXDB_ADMIN_TOKEN}
    ports:
      - "${INFLUXDB_PORT}:8086"
    volumes:
      - ./influxdb/data:/var/lib/influxdb2

  # --- Keycloak ---
  keycloakDB:
    image: postgis/postgis:14-3.5
    container_name: keycloakDB
    restart: always
    environment:
      POSTGRES_DB: ${KEYCLOAK_DB_NAME}
      POSTGRES_USER: ${KEYCLOAK_DB_USER}
      POSTGRES_PASSWORD: ${KEYCLOAK_DB_PASSWORD}
    command: postgres -c wal_level=logical
    ports:
      - "${KEYCLOAK_DB_PORT}:5432"
    volumes:
      - ./keycloak/db_data:/var/lib/postgresql/data

  keycloak:
    image: keycloak/keycloak:latest
    container_name: keycloak
    restart: always
    environment:
      KC_HOSTNAME: localhost
      KC_HOSTNAME_STRICT_BACKCHANNEL: "true"
      KEYCLOAK_ADMIN: ${KEYCLOAK_ADMIN_USER}
      KEYCLOAK_ADMIN_PASSWORD: ${KEYCLOAK_ADMIN_PASSWORD}
      KC_HEALTH_ENABLED: "true"
      KC_LOG_LEVEL: info
      KC_DB: postgres
      KC_DB_URL: jdbc:postgresql://keycloakDB:5432/${KEYCLOAK_DB_NAME}
      KC_DB_USERNAME: ${KEYCLOAK_DB_USER}
      KC_DB_PASSWORD: ${KEYCLOAK_DB_PASSWORD}
    volumes:
      - ./keycloak/themes:/opt/keycloak/themes
      - ./keycloak/import:/opt/keycloak/data/import
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:8080/health/ready" ]
      interval: 15s
      timeout: 2s
      retries: 15
    command: [ "start-dev", "--import-realm" ]
    ports:
      - "${KEYCLOAK_PORT}:8080"
    depends_on:
      - keycloakDB

  # --- TimescaleDB & Grafana ---
  timescaledb:
    image: timescale/timescaledb:latest-pg15
    container_name: timescaledb
    restart: always
    environment:
      POSTGRES_DB: ${TIMESCALE_DB_NAME}
      POSTGRES_USER: ${TIMESCALE_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD_SHARED}
    ports:
      - "${TIMESCALE_PORT}:5432"
    volumes:
      - ./timescaledb/data:/var/lib/postgresql/data

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    restart: always
    ports:
      - "${GRAFANA_PORT}:3000"
    depends_on:
      - timescaledb
    volumes:
      - ./timescaledb/grafana_data:/var/lib/grafana
    environment:
      - GF_SECURITY_ADMIN_USER=${GRAFANA_ADMIN_USER}
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD}

  # --- MapService (PostGIS & GeoServer) ---
  postgis:
    image: postgis/postgis:14-3.5
    container_name: postgis
    restart: always
    environment:
      POSTGRES_DB: ${POSTGIS_DB_NAME}
      POSTGRES_USER: ${POSTGIS_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD_SHARED}
    ports:
      - "${POSTGIS_PORT}:5432"
    volumes:
      - ./mapservice/data:/var/lib/postgresql/data

  geoserver:
    image: docker.osgeo.org/geoserver:2.27.1
    container_name: geoserver
    restart: always
    ports:
      - "${GEOSERVER_PORT}:8080"
    depends_on:
      - postgis
    environment:
      - GEOSERVER_ADMIN_USER=${GEOSERVER_ADMIN_USER}
      - GEOSERVER_ADMIN_PASSWORD=${GEOSERVER_ADMIN_PASSWORD}
      - INSTALL_EXTENSIONS=true
      - STABLE_EXTENSIONS=vectortiles
      - CORS_ENABLED=true
      - CORS_ALLOWED_ORIGINS=*
    volumes:
      - ./mapservice/geoserver_data:/opt/geoserver_data

networks:
  default:
    driver: bridge
