# compose.dev.yml
# ============================================================================
# DEVELOPMENT DOCKER COMPOSE CONFIGURATION
# ============================================================================
# This file defines the local development environment using Docker/Podman.
#
# Services:
#   - app: Node.js application (API + Frontend + Bugsink + Logstash)
#   - postgres: PostgreSQL 15 with PostGIS extension
#   - redis: Redis for caching and job queues
#
# Usage:
#   Start all services:     podman-compose -f compose.dev.yml up -d
#   Stop all services:      podman-compose -f compose.dev.yml down
#   View logs:              podman-compose -f compose.dev.yml logs -f
#   Reset everything:       podman-compose -f compose.dev.yml down -v
#
# VS Code Dev Containers:
#   This file is referenced by .devcontainer/devcontainer.json for seamless
#   VS Code integration. Open the project in VS Code and use "Reopen in Container".
#
# Bugsink (ADR-015):
#   Access error tracking UI at http://localhost:8000
#   Default login: admin@localhost / admin
# ============================================================================

version: '3.8'

services:
  # ===================
  # Application Service
  # ===================
  app:
    container_name: flyer-crawler-dev
    # Use pre-built image if available, otherwise build from Dockerfile.dev
    # To build: podman build -f Dockerfile.dev -t flyer-crawler-dev:latest .
    image: localhost/flyer-crawler-dev:latest
    build:
      context: .
      dockerfile: Dockerfile.dev
    volumes:
      # Mount the current directory to /app in the container
      - .:/app
      # Create a volume for node_modules to avoid conflicts with Windows host
      # and improve performance.
      - node_modules_data:/app/node_modules
    ports:
      - '3000:3000' # Frontend (Vite default)
      - '3001:3001' # Backend API
      - '8000:8000' # Bugsink error tracking (ADR-015)
    environment:
      # Core settings
      - NODE_ENV=development
      # Database - use service name for Docker networking
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_PASSWORD=postgres
      - DB_NAME=flyer_crawler_dev
      # Redis - use service name for Docker networking
      - REDIS_URL=redis://redis:6379
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      # Frontend URL for CORS
      - FRONTEND_URL=http://localhost:3000
      # Default JWT secret for development (override in production!)
      - JWT_SECRET=dev-jwt-secret-change-in-production
      # Worker settings
      - WORKER_LOCK_DURATION=120000
      # Bugsink error tracking (ADR-015)
      - BUGSINK_DB_HOST=postgres
      - BUGSINK_DB_PORT=5432
      - BUGSINK_DB_NAME=bugsink
      - BUGSINK_DB_USER=bugsink
      - BUGSINK_DB_PASSWORD=bugsink_dev_password
      - BUGSINK_PORT=8000
      - BUGSINK_BASE_URL=http://localhost:8000
      - BUGSINK_ADMIN_EMAIL=admin@localhost
      - BUGSINK_ADMIN_PASSWORD=admin
      - BUGSINK_SECRET_KEY=dev-bugsink-secret-key-minimum-50-characters-for-security
      # Sentry SDK configuration (points to local Bugsink)
      - SENTRY_DSN=http://59a58583-e869-7697-f94a-cfa0337676a8@localhost:8000/1
      - VITE_SENTRY_DSN=http://d5fc5221-4266-ff2f-9af8-5689696072f3@localhost:8000/2
      - SENTRY_ENVIRONMENT=development
      - VITE_SENTRY_ENVIRONMENT=development
      - SENTRY_ENABLED=true
      - VITE_SENTRY_ENABLED=true
      - SENTRY_DEBUG=true
      - VITE_SENTRY_DEBUG=true
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    # Keep container running so VS Code can attach
    command: tail -f /dev/null
    # Healthcheck for the app (once it's running)
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:3001/api/health', '||', 'exit', '0']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # ===================
  # PostgreSQL Database
  # ===================
  postgres:
    image: docker.io/postgis/postgis:15-3.4
    container_name: flyer-crawler-postgres
    ports:
      - '5432:5432'
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: flyer_crawler_dev
      # Optimize for development
      POSTGRES_INITDB_ARGS: '--encoding=UTF8 --locale=C'
    volumes:
      - postgres_data:/var/lib/postgresql/data
      # Mount init scripts to run on first database creation
      # Scripts run in alphabetical order: 00-extensions, 01-bugsink
      - ./sql/00-init-extensions.sql:/docker-entrypoint-initdb.d/00-init-extensions.sql:ro
      - ./sql/01-init-bugsink.sh:/docker-entrypoint-initdb.d/01-init-bugsink.sh:ro
    # Healthcheck ensures postgres is ready before app starts
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U postgres -d flyer_crawler_dev']
      interval: 5s
      timeout: 5s
      retries: 10
      start_period: 10s

  # ===================
  # Redis Cache/Queue
  # ===================
  redis:
    image: docker.io/library/redis:alpine
    container_name: flyer-crawler-redis
    ports:
      - '6379:6379'
    volumes:
      - redis_data:/data
    # Healthcheck ensures redis is ready before app starts
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 5s
      timeout: 5s
      retries: 10
      start_period: 5s
    # Enable persistence for development data
    command: redis-server --appendonly yes

# ===================
# Named Volumes
# ===================
volumes:
  postgres_data:
    name: flyer-crawler-postgres-data
  redis_data:
    name: flyer-crawler-redis-data
  node_modules_data:
    name: flyer-crawler-node-modules

# ===================
# Network Configuration
# ===================
# All services are on the default bridge network.
# Use service names (postgres, redis) as hostnames.
