# =============================================================================
# MOPC Platform - Production Docker Compose
# =============================================================================
# This stack contains only the Next.js app and PostgreSQL.
# MinIO and Poste.io are external services connected via environment variables.
#
# The app image is built by Gitea CI and pushed to the container registry.
# To pull the latest image: docker compose pull app
# To deploy: docker compose up -d

services:
  app:
    image: ${REGISTRY_URL}/mopc-app:latest
    container_name: mopc-app
    restart: unless-stopped
    dns:
      - 8.8.8.8
      - 8.8.4.4
    ports:
      - "127.0.0.1:7600:7600"
    env_file:
      - .env
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgresql://mopc:${DB_PASSWORD}@postgres:5432/mopc
      - NEXTAUTH_URL=${NEXTAUTH_URL}
      - NEXTAUTH_SECRET=${NEXTAUTH_SECRET}
      - AUTH_SECRET=${NEXTAUTH_SECRET}
      - AUTH_TRUST_HOST=true
      - MINIO_ENDPOINT=${MINIO_ENDPOINT}
      - MINIO_PUBLIC_ENDPOINT=${MINIO_PUBLIC_ENDPOINT:-}
      - MINIO_ACCESS_KEY=${MINIO_ACCESS_KEY}
      - MINIO_SECRET_KEY=${MINIO_SECRET_KEY}
      - MINIO_BUCKET=${MINIO_BUCKET}
      - SMTP_HOST=${SMTP_HOST}
      - SMTP_PORT=${SMTP_PORT}
      - SMTP_USER=${SMTP_USER}
      - SMTP_PASS=${SMTP_PASS}
      - EMAIL_FROM=${EMAIL_FROM}
      - POSTE_API_URL=${POSTE_API_URL:-https://mail.monaco-opc.com}
      - POSTE_ADMIN_EMAIL=${POSTE_ADMIN_EMAIL}
      - POSTE_ADMIN_PASSWORD=${POSTE_ADMIN_PASSWORD}
      - POSTE_MAIL_DOMAIN=${POSTE_MAIL_DOMAIN:-monaco-opc.com}
      - OPENAI_API_KEY=${OPENAI_API_KEY:-}
      - OPENAI_MODEL=${OPENAI_MODEL:-gpt-4o}
      - MAX_FILE_SIZE=${MAX_FILE_SIZE:-524288000}
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - mopc-network
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:7600/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  postgres:
    image: postgres:16-alpine
    container_name: mopc-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_USER=mopc
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=mopc
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U mopc"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - mopc-network

volumes:
  postgres_data:
    driver: local

networks:
  mopc-network:
    driver: bridge
