# ============================================================
#  SiGeM — Docker Compose (Produção)
#  Município de Moatize — Versão 1.0
# ============================================================

version: '3.9'

services:

  # ── PostgreSQL 15 ────────────────────────────────────────
  postgres:
    image: postgres:15-alpine
    container_name: sigem_postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB:       sigem_moatize
      POSTGRES_USER:     sigem_user
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      PGDATA:            /var/lib/postgresql/data/pgdata
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./database/01_schema.sql:/docker-entrypoint-initdb.d/01_schema.sql
      - ./database/02_seed.sql:/docker-entrypoint-initdb.d/02_seed.sql
      - ./database/03_views_functions.sql:/docker-entrypoint-initdb.d/03_views_functions.sql
      - ./backups:/backups
    ports:
      - "5432:5432"
    networks:
      - sigem_net
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U sigem_user -d sigem_moatize"]
      interval: 10s
      timeout: 5s
      retries: 5
    command: >
      postgres
        -c max_connections=200
        -c shared_buffers=256MB
        -c effective_cache_size=1GB
        -c maintenance_work_mem=128MB
        -c checkpoint_completion_target=0.9
        -c wal_buffers=16MB
        -c default_statistics_target=100
        -c log_min_duration_statement=1000
        -c log_line_prefix='%t [%p]: [%l-1] user=%u,db=%d,app=%a '

  # ── Redis (sessões e cache) ──────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: sigem_redis
    restart: unless-stopped
    command: redis-server --requirepass ${REDIS_PASSWORD} --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    networks:
      - sigem_net
    healthcheck:
      test: ["CMD", "redis-cli", "--no-auth-warning", "-a", "${REDIS_PASSWORD}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 3

  # ── Backend Node.js ──────────────────────────────────────
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: sigem_backend
    restart: unless-stopped
    environment:
      NODE_ENV:         production
      PORT:             3000
      DB_HOST:          postgres
      DB_PORT:          5432
      DB_NAME:          sigem_moatize
      DB_USER:          sigem_user
      DB_PASSWORD:      ${DB_PASSWORD}
      JWT_SECRET:       ${JWT_SECRET}
      REDIS_HOST:       redis
      REDIS_PORT:       6379
      REDIS_PASSWORD:   ${REDIS_PASSWORD}
      SMTP_HOST:        ${SMTP_HOST}
      SMTP_USER:        ${SMTP_USER}
      SMTP_PASS:        ${SMTP_PASS}
      CORS_ORIGINS:     ${CORS_ORIGINS}
      QR_BASE_URL:      ${QR_BASE_URL}
    volumes:
      - ./uploads:/app/uploads
      - ./logs:/app/logs
      - ./backups:/app/backups
    ports:
      - "3000:3000"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - sigem_net
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  # ── Frontend React (Nginx) ────────────────────────────────
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        VITE_API_URL: ${VITE_API_URL:-http://localhost:3000/api/v1}
        VITE_APP_NAME: SiGeM Moatize
    container_name: sigem_frontend
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/sigem.conf:/etc/nginx/conf.d/default.conf:ro
      - ./ssl:/etc/ssl/sigem:ro
    depends_on:
      - backend
    networks:
      - sigem_net

  # ── Backup automático ────────────────────────────────────
  backup:
    image: postgres:15-alpine
    container_name: sigem_backup
    restart: unless-stopped
    environment:
      POSTGRES_HOST:     postgres
      POSTGRES_DB:       sigem_moatize
      POSTGRES_USER:     sigem_user
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - ./backups:/backups
      - ./docker/backup.sh:/backup.sh:ro
    entrypoint: ["/bin/sh", "-c", "while true; do sleep 86400; /backup.sh; done"]
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - sigem_net

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

networks:
  sigem_net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
