# LocalGreenChain Production Docker Compose
# Agent 4: Production Deployment
# Full stack with PostgreSQL, Redis, and monitoring

version: '3.8'

services:
  # ==========================================================================
  # Application
  # ==========================================================================
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL:-http://localhost:3001}
        - NEXT_PUBLIC_SENTRY_DSN=${NEXT_PUBLIC_SENTRY_DSN:-}
    container_name: lgc-app
    restart: unless-stopped
    ports:
      - "${PORT:-3001}:3001"
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgresql://${DB_USER:-lgc}:${DB_PASSWORD:-lgc_password}@postgres:5432/${DB_NAME:-localgreenchain}
      - REDIS_URL=redis://redis:6379
      - SENTRY_DSN=${SENTRY_DSN:-}
      - LOG_LEVEL=${LOG_LEVEL:-info}
      - PLANTS_NET_API_KEY=${PLANTS_NET_API_KEY:-}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - app-data:/app/data
    networks:
      - lgc-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3001/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    labels:
      - "prometheus.scrape=true"
      - "prometheus.port=3001"
      - "prometheus.path=/api/metrics"

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

  # ==========================================================================
  # Cache
  # ==========================================================================
  redis:
    image: redis:7-alpine
    container_name: lgc-redis
    restart: unless-stopped
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - redis-data:/data
    ports:
      - "${REDIS_PORT:-6379}:6379"
    networks:
      - lgc-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ==========================================================================
  # Monitoring - Prometheus
  # ==========================================================================
  prometheus:
    image: prom/prometheus:v2.47.0
    container_name: lgc-prometheus
    restart: unless-stopped
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--storage.tsdb.retention.time=15d'
      - '--web.enable-lifecycle'
    volumes:
      - ./infra/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus-data:/prometheus
    ports:
      - "${PROMETHEUS_PORT:-9090}:9090"
    networks:
      - lgc-network
    depends_on:
      - app
    profiles:
      - monitoring

  # ==========================================================================
  # Monitoring - Grafana
  # ==========================================================================
  grafana:
    image: grafana/grafana:10.1.0
    container_name: lgc-grafana
    restart: unless-stopped
    environment:
      - GF_SECURITY_ADMIN_USER=${GRAFANA_USER:-admin}
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD:-admin}
      - GF_USERS_ALLOW_SIGN_UP=false
      - GF_SERVER_ROOT_URL=${GRAFANA_ROOT_URL:-http://localhost:3000}
    volumes:
      - grafana-data:/var/lib/grafana
      - ./infra/grafana/provisioning:/etc/grafana/provisioning:ro
      - ./infra/grafana/dashboards:/var/lib/grafana/dashboards:ro
    ports:
      - "${GRAFANA_PORT:-3000}:3000"
    networks:
      - lgc-network
    depends_on:
      - prometheus
    profiles:
      - monitoring

# =============================================================================
# Networks
# =============================================================================
networks:
  lgc-network:
    driver: bridge
    name: lgc-network

# =============================================================================
# Volumes
# =============================================================================
volumes:
  app-data:
    name: lgc-app-data
  postgres-data:
    name: lgc-postgres-data
  redis-data:
    name: lgc-redis-data
  prometheus-data:
    name: lgc-prometheus-data
  grafana-data:
    name: lgc-grafana-data
