services:
  # PostgreSQL 16 - Primary Database
  postgres:
    image: postgres:16-alpine
    container_name: cbt-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_NAME:-cbt}
      POSTGRES_USER: ${DB_USER:-postgres}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - "${DB_PORT:-5432}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-cbt}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    networks:
      - cbt-network

  # Redis 7 - Caching and Sessions
  redis:
    image: redis:7-alpine
    container_name: cbt-redis
    restart: unless-stopped
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 10s
    networks:
      - cbt-network

  # Golang API with Hot Reload
  api:
    build:
      context: ./cbt-backend
      dockerfile: Dockerfile.dev
    container_name: cbt-api
    restart: unless-stopped
    environment:
      ENVIRONMENT: development
      SERVER_PORT: 8080
      DB_HOST: postgres
      DB_PORT: 5432
      DB_USER: ${DB_USER:-postgres}
      DB_PASSWORD: ${DB_PASSWORD:-postgres}
      DB_NAME: ${DB_NAME:-cbt}
      DB_SSLMODE: disable
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 0
    ports:
      - "${API_PORT:-8080}:8080"
    volumes:
      - ./cbt-backend:/app
      - go_modules:/go/pkg/mod
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://127.0.0.1:8080/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    networks:
      - cbt-network

  # Adminer - Database Management UI
  adminer:
    image: adminer:latest
    container_name: cbt-adminer
    restart: unless-stopped
    environment:
      ADMINER_DEFAULT_SERVER: postgres
      ADMINER_DESIGN: nette
    ports:
      - "${ADMINER_PORT:-8081}:8080"
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - cbt-network

networks:
  cbt-network:
    driver: bridge

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  go_modules:
    driver: local
