# =============================================================================
# IGNY8 APP-LEVEL COMPOSE FILE
# =============================================================================
# This is the compose file for the IGNY8 app stack (separate from infra)
# Used by both Portainer and CLI - no conflicts
# =============================================================================
# 
# Usage:
# cd /data/app
# docker compose -f docker-compose.yml -p igny8-infra up -d
# cd /data/app/igny8
# docker compose -f docker-compose.app.yml -p igny8-app up -d

# NOTE: Images must be built separately before using:
#   cd /data/app/igny8/backend && docker build -t igny8-backend:latest -f Dockerfile .
#   cd /data/app/igny8/frontend && docker build -t igny8-frontend-dev:latest -f Dockerfile.dev .
#   cd /data/app/igny8/frontend && docker build -t igny8-marketing-dev:latest -f Dockerfile.marketing.dev .
# =============================================================================

name: igny8-app

services:
  igny8_backend:
    # NOTE: Use 'image:' not 'build:' to avoid creating parallel stacks
    # Build images separately: docker build -t igny8-backend:latest -f Dockerfile .
    image: igny8-backend:latest
    container_name: igny8_backend
    restart: always
    working_dir: /app
    ports:
      - "0.0.0.0:8011:8010"
    environment:
      DB_HOST: postgres
      DB_NAME: igny8_db
      DB_USER: igny8
      DB_PASSWORD: igny8pass
      REDIS_HOST: redis
      REDIS_PORT: "6379"
      # Enable secure cookies and proxy headers for production (behind Caddy HTTPS)
      USE_SECURE_COOKIES: "True"
      USE_SECURE_PROXY_HEADER: "True"
      # Security settings - set via environment variables in production
      DEBUG: "False"
      # SECRET_KEY should be set via environment variable in production
    volumes:    
      - /data/app/igny8/backend:/app:rw      
      - /data/app/igny8:/data/app/igny8:rw      
      - /var/run/docker.sock:/var/run/docker.sock:ro      
      - /data/app/logs:/app/logs:rw
    # Note: postgres and redis are external services from infra stack
    # They're on the same network, but we can't use depends_on for external services
    # Ensure they're running before starting this stack
    healthcheck:
      test: ["CMD-SHELL", "python -c \"import urllib.request; urllib.request.urlopen('http://localhost:8010/api/v1/system/status/').read()\" || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    command: ["gunicorn", "igny8_core.wsgi:application", "--bind", "0.0.0.0:8010", "--workers", "4", "--timeout", "120"]
    networks: [igny8_net]
    labels:
      - "com.docker.compose.project=igny8-app"
      - "com.docker.compose.service=igny8_backend"

  igny8_frontend:
    # NOTE: Use 'image:' not 'build:' to avoid creating parallel stacks
    # Build images separately: docker build -t igny8-frontend-dev:latest -f Dockerfile.dev .
    image: igny8-frontend-dev:latest
    container_name: igny8_frontend
    restart: always
    ports:
      - "0.0.0.0:8021:5173"  # Vite dev server port
    environment:
      VITE_BACKEND_URL: "https://api.igny8.com/api"
    volumes:      
      - /data/app/igny8/frontend:/app:rw     
    depends_on:
      igny8_backend:
        condition: service_healthy
    networks: [igny8_net]
    labels:
      - "com.docker.compose.project=igny8-app"
      - "com.docker.compose.service=igny8_frontend"

  igny8_marketing_dev:
    # Development server for marketing site with HMR
    # Build separately: docker build -t igny8-marketing-dev:latest -f Dockerfile.marketing.dev .
    # Dev server accessible at http://localhost:8023 (direct) or via Caddy routing (when configured)
    image: igny8-marketing-dev:latest
    container_name: igny8_marketing_dev
    restart: always
    ports:
      - "0.0.0.0:8023:5174"  # Marketing dev server port (internal: 5174, external: 8023)
    environment:
      VITE_BACKEND_URL: "https://api.igny8.com/api"
    volumes:
      - /data/app/igny8/frontend:/app:rw
    networks: [igny8_net]
    labels:
      - "com.docker.compose.project=igny8-app"
      - "com.docker.compose.service=igny8_marketing_dev"

  igny8_celery_worker:
    image: igny8-backend:latest
    container_name: igny8_celery_worker
    restart: always
    working_dir: /app
    environment:
      DB_HOST: postgres
      DB_NAME: igny8_db
      DB_USER: igny8
      DB_PASSWORD: igny8pass
      REDIS_HOST: redis
      REDIS_PORT: "6379"
      DEBUG: "False"
    volumes:
      - /data/app/igny8/backend:/app:rw
      - /data/app/igny8:/data/app/igny8:rw
      - /data/app/logs:/app/logs:rw
    # Note: postgres and redis are external services from infra stack
    # Ensure they're running before starting this stack
    command: ["celery", "-A", "igny8_core", "worker", "--loglevel=info", "--concurrency=4"]
    networks: [igny8_net]
    labels:
      - "com.docker.compose.project=igny8-app"
      - "com.docker.compose.service=igny8_celery_worker"

  igny8_celery_beat:
    image: igny8-backend:latest
    container_name: igny8_celery_beat
    restart: always
    working_dir: /app
    environment:
      DB_HOST: postgres
      DB_NAME: igny8_db
      DB_USER: igny8
      DB_PASSWORD: igny8pass
      REDIS_HOST: redis
      REDIS_PORT: "6379"
      DEBUG: "False"
    volumes:
      - /data/app/igny8/backend:/app:rw
      - /data/app/igny8:/data/app/igny8:ro
      - /data/app/logs:/app/logs:rw
    # Note: postgres and redis are external services from infra stack
    # Ensure they're running before starting this stack
    command: ["celery", "-A", "igny8_core", "beat", "--loglevel=info"]
    networks: [igny8_net]
    labels:
      - "com.docker.compose.project=igny8-app"
      - "com.docker.compose.service=igny8_celery_beat"

networks:
  igny8_net:
    external: true

# Note: postgres and redis services are defined in /data/app/docker-compose.yml
# They must be running before starting this stack
