# Changemaker Control Panel — Docker Compose
# Start: docker compose up -d

services:
  ccp-postgres:
    image: ${GITEA_REGISTRY:-gitea.bnkops.com/admin}/postgres:16-alpine
    container_name: ccp-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ccp
      POSTGRES_PASSWORD: ${CCP_POSTGRES_PASSWORD:-ccp_secret}
      POSTGRES_DB: ccp
    volumes:
      - ccp-postgres-data:/var/lib/postgresql/data
    ports:
      - "127.0.0.1:${CCP_POSTGRES_PORT:-5480}:5432"
    networks:
      - ccp-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ccp -d ccp"]
      interval: 10s
      timeout: 5s
      retries: 5

  ccp-redis:
    image: ${GITEA_REGISTRY:-gitea.bnkops.com/admin}/redis:7-alpine
    container_name: ccp-redis
    restart: unless-stopped
    command: redis-server --requirepass ${REDIS_PASSWORD:-ccp_redis_secret}
    volumes:
      - ccp-redis-data:/data
    ports:
      - "127.0.0.1:${CCP_REDIS_PORT:-6399}:6379"
    networks:
      - ccp-network
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD:-ccp_redis_secret}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  ccp-api:
    build:
      context: .
      dockerfile: Dockerfile.api
    container_name: ccp-api
    restart: unless-stopped
    depends_on:
      ccp-postgres:
        condition: service_healthy
      ccp-redis:
        condition: service_healthy
    env_file: .env
    environment:
      DATABASE_URL: postgresql://ccp:${CCP_POSTGRES_PASSWORD:-ccp_secret}@ccp-postgres:5432/ccp
      REDIS_URL: redis://:${REDIS_PASSWORD:-ccp_redis_secret}@ccp-redis:6379
    ports:
      - "${CCP_API_PORT:-5000}:5000"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ./api:/app
      - /app/node_modules
      - ./templates:/app/templates:ro
      - /var/run/docker.sock:/var/run/docker.sock
      - ${INSTANCES_BASE_PATH}:${INSTANCES_BASE_PATH}
      - ${CML_SOURCE_PATH}:${CML_SOURCE_PATH}:ro
      - ${BACKUP_STORAGE_PATH}:${BACKUP_STORAGE_PATH}
    networks:
      - ccp-network

  ccp-admin:
    build:
      context: .
      dockerfile: Dockerfile.admin
    container_name: ccp-admin
    restart: unless-stopped
    depends_on:
      - ccp-api
    ports:
      - "${CCP_ADMIN_PORT:-5100}:5100"
    networks:
      - ccp-network

volumes:
  ccp-postgres-data:
  ccp-redis-data:

networks:
  ccp-network:
    driver: bridge
