services:
  # PostgreSQL for sample data and results storage
  postgres:
    image: postgres:15
    container_name: baselinr_postgres
    environment:
      POSTGRES_USER: baselinr
      POSTGRES_PASSWORD: baselinr
      POSTGRES_DB: baselinr
    ports:
      - "5433:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./init_postgres.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U baselinr"]
      interval: 5s
      timeout: 5s
      retries: 5

  # Dagster daemon for scheduling
  dagster_daemon:
    build:
      context: ..
      dockerfile: docker/Dockerfile
    container_name: baselinr_dagster_daemon
    environment:
      - DAGSTER_HOME=/opt/dagster/dagster_home
      - DAGSTER_POSTGRES_URL=postgresql://baselinr:baselinr@postgres:5432/baselinr
      # Baselinr config overrides for Docker
      - BASELINR_SOURCE__HOST=postgres
      - BASELINR_SOURCE__PORT=5432
      - BASELINR_STORAGE__CONNECTION__HOST=postgres
      - BASELINR_STORAGE__CONNECTION__PORT=5432
    volumes:
      - ../:/app
      - dagster_home:/opt/dagster/dagster_home
    depends_on:
      postgres:
        condition: service_healthy
    command: dagster-daemon run -w /opt/dagster/dagster_home/workspace.yaml

  # Dagster web UI
  dagster_webserver:
    build:
      context: ..
      dockerfile: docker/Dockerfile
    container_name: baselinr_dagster_webserver
    environment:
      - DAGSTER_HOME=/opt/dagster/dagster_home
      - DAGSTER_POSTGRES_URL=postgresql://baselinr:baselinr@postgres:5432/baselinr
      # Baselinr config overrides for Docker
      - BASELINR_SOURCE__HOST=postgres
      - BASELINR_SOURCE__PORT=5432
      - BASELINR_STORAGE__CONNECTION__HOST=postgres
      - BASELINR_STORAGE__CONNECTION__PORT=5432
    ports:
      - "3000:3000"
    volumes:
      - ../:/app
      - dagster_home:/opt/dagster/dagster_home
    depends_on:
      postgres:
        condition: service_healthy
    command: dagster-webserver -h 0.0.0.0 -p 3000 -w /opt/dagster/dagster_home/workspace.yaml

  # Prometheus for metrics collection
  prometheus:
    image: prom/prometheus:latest
    container_name: baselinr_prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
      - '--storage.tsdb.retention.time=30d'
    depends_on:
      - postgres
    networks:
      - default
    extra_hosts:
      - "host.docker.internal:host-gateway"  # Allows access to host machine

  # Grafana for metrics visualization
  grafana:
    image: grafana/grafana:latest
    container_name: baselinr_grafana
    ports:
      - "3001:3000"  # Changed from 3000 to avoid conflict with Dagster
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_USERS_ALLOW_SIGN_UP=false
      - GF_INSTALL_PLUGINS=
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana-datasources.yml:/etc/grafana/provisioning/datasources/datasources.yml
      - ./grafana-dashboards:/var/lib/grafana/dashboards
    depends_on:
      - prometheus

volumes:
  postgres_data:
  dagster_home:
  prometheus_data:
  grafana_data:

