services:
  hctv:
    container_name: hctv
    depends_on:
      postgres:
        condition: service_healthy
      pgbouncer:
        condition: service_started
    env_file:
      - .env
    build:
      context: .
      dockerfile: apps/web/Dockerfile
  chat:
    depends_on:
      postgres:
        condition: service_healthy
      hctv:
        condition: service_started
    env_file:
      - .env
    build:
      context: .
      dockerfile: apps/chat/Dockerfile
  postgres:
    image: 'postgres:17-alpine'
    ports:
      - '6767:5432'
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: '${PG_PASS}'
      POSTGRES_DB: hctv
    volumes:
      - 'hctv_pgdata:/var/lib/postgresql/data'
    healthcheck:
      test:
        - CMD-SHELL
        - 'pg_isready -U postgres'
      interval: 5s
      timeout: 5s
      retries: 5
  pgbouncer:
    image: 'bitnamilegacy/pgbouncer:1'
    environment:
      - POSTGRESQL_HOST=postgres
      - POSTGRESQL_PORT=5432
      - POSTGRESQL_USERNAME=postgres
      - 'POSTGRESQL_PASSWORD=${PG_PASS}'
      - PGBOUNCER_DATABASE=hctv
      - POSTGRESQL_DATABASE=hctv
      - PGBOUNCER_POOL_MODE=transaction
      - PGBOUNCER_MAX_CLIENT_CONN=100
      - PGBOUNCER_DEFAULT_POOL_SIZE=20
    depends_on:
      - postgres
  redis:
    image: 'redis:7.4-alpine'
    volumes:
      - 'hctv_redis:/data'
  mediamtx:
    build:
      context: .
      dockerfile: docker/mediamtx/Dockerfile
    ports:
      - '8890:8890/udp'
  postgres-exporter:
    image: 'prometheuscommunity/postgres-exporter:v0.17.1'
    environment:
      DATA_SOURCE_NAME: 'postgresql://postgres:${PG_PASS}@postgres:5432/hctv?sslmode=disable'
  redis-exporter:
    image: 'oliver006/redis_exporter:v1.67.0'
    environment:
      REDIS_ADDR: 'redis://redis:6379'
  prometheus:
    image: 'prom/prometheus:v3.4.2'
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.enable-lifecycle'
    volumes:
      - './observability/prometheus.yml:/etc/prometheus/prometheus.yml:ro'
      - './observability/web_username:/etc/prometheus/web_username'
      - './observability/web_password:/etc/prometheus/web_password'
      - 'hctv_prometheus_data:/prometheus'
    extra_hosts:
      - 'host.docker.internal:host-gateway'
  grafana:
    image: 'grafana/grafana:11.6.0'
    depends_on:
      - prometheus
    environment:
      GF_SECURITY_ADMIN_USER: '${GRAFANA_ADMIN_USER:-admin}'
      GF_SECURITY_ADMIN_PASSWORD: '${GRAFANA_ADMIN_PASSWORD:-admin}'
      GF_USERS_DEFAULT_THEME: light
    volumes:
      - './observability/grafana/provisioning/datasources:/etc/grafana/provisioning/datasources:ro'
      - './observability/grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards:ro'
      - './observability/grafana/dashboards:/var/lib/grafana/dashboards:ro'
      - 'hctv_grafana_data:/var/lib/grafana'

volumes:
  hctv_pgdata:
  hctv_redis:
  hctv_prometheus_data:
  hctv_grafana_data:
