services:
  # PostgreSQL for Airflow metadata
  postgres-metadata:
    image: postgres:16
    environment:
      POSTGRES_USER: airflow
      POSTGRES_PASSWORD: airflow
      POSTGRES_DB: airflow
    ports:
      - "5434:5432"
    volumes:
      - pgmeta:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U airflow -d airflow"]
      interval: 5s
      timeout: 5s
      retries: 20

  # PostgreSQL for training exercises
  postgres-training:
    image: postgres:16
    environment:
      POSTGRES_USER: student
      POSTGRES_PASSWORD: student
      POSTGRES_DB: training
    ports:
      - "5432:5432"
    volumes:
      - pg_data:/var/lib/postgresql/data
      - ./init-postgres.sql:/docker-entrypoint-initdb.d/init-postgres.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U student -d training"]
      interval: 5s
      timeout: 5s
      retries: 20

  # Airflow webserver
  airflow-webserver:
    build: .
    image: airflow-optimized:2.9.2
    environment:
      AIRFLOW__CORE__LOAD_EXAMPLES: "False"
      AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql+psycopg2://airflow:airflow@postgres-metadata:5432/airflow
      AIRFLOW__CORE__EXECUTOR: LocalExecutor
      AIRFLOW__WEBSERVER__SECRET_KEY: replace-me-with-random-string
    command: >
      bash -c "
        airflow webserver
      "
    ports:
      - "8080:8080"
    volumes:
      - ./dags:/opt/airflow/dags
      - ./data:/opt/airflow/data
    depends_on:
      airflow-init:
        condition: service_completed_successfully
      postgres-metadata:
        condition: service_healthy
      postgres-training:
        condition: service_healthy

  # Airflow scheduler
  airflow-scheduler:
    build: .
    # image: airflow-optimized:2.9.2
    environment:
      AIRFLOW__CORE__LOAD_EXAMPLES: "False"
      AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql+psycopg2://airflow:airflow@postgres-metadata:5432/airflow
      AIRFLOW__CORE__EXECUTOR: LocalExecutor
      AIRFLOW__WEBSERVER__SECRET_KEY: replace-me-with-random-string
    command: >
      bash -c "
        airflow scheduler
      "
    volumes:
      - ./dags:/opt/airflow/dags
      - ./data:/opt/airflow/data
    depends_on:
      airflow-init:
        condition: service_completed_successfully
      postgres-metadata:
        condition: service_healthy
      postgres-training:
        condition: service_healthy

  # Airflow init to create admin user
  airflow-init:
    build: .
    user: "0:0"
    # image: airflow-optimized:2.9.2
    environment:
      AIRFLOW__CORE__LOAD_EXAMPLES: "False"
      AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql+psycopg2://airflow:airflow@postgres-metadata:5432/airflow
      AIRFLOW__WEBSERVER__SECRET_KEY: replace-me-with-random-string
    volumes:
      - ./dags:/opt/airflow/dags
      - ./data:/opt/airflow/data
    command: >
      bash -ceuo pipefail "
        mkdir -p /opt/airflow/data &&
        chmod -R 777 /opt/airflow/data || true &&
        chown -R airflow:0 /opt/airflow/data || true &&
        umask 000 &&
        su -s /bin/bash airflow -c 'airflow db migrate' &&
        su -s /bin/bash airflow -c 'airflow users create --username admin --password admin --firstname Admin --lastname User --role Admin --email admin@example.org'
      "
    depends_on:
      postgres-metadata:
        condition: service_healthy

volumes:
  pgmeta:
  pg_data:
