version: '3.8'

services:
  # ═══════════════════════════════════════
  # BANCO DE DADOS POSTGRESQL
  # ═══════════════════════════════════════
  postgres:
    image: postgres:16-alpine
    container_name: pdimaker-db
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: pdimaker_prod
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks:
      - pdimaker-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ═══════════════════════════════════════
  # REDIS (Cache e Sessões)
  # ═══════════════════════════════════════
  redis:
    image: redis:7-alpine
    container_name: pdimaker-redis
    restart: always
    command: redis-server --requirepass ${REDIS_PASSWORD}
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    networks:
      - pdimaker-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ═══════════════════════════════════════
  # BACKEND API (NestJS)
  # ═══════════════════════════════════════
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: pdimaker-api
    restart: always
    environment:
      NODE_ENV: production
      PORT: 4000
      DATABASE_URL: postgresql://postgres:${DB_PASSWORD}@postgres:5432/pdimaker_prod
      REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379
      JWT_SECRET: ${JWT_SECRET}
      GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID}
      GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET}
      FRONTEND_URL: https://pdimaker.com.br
    volumes:
      - ./backend:/app
      - /app/node_modules
      - uploads_data:/app/uploads
    ports:
      - "4000:4000"
    depends_on:
      - postgres
      - redis
    networks:
      - pdimaker-network

  # ═══════════════════════════════════════
  # FRONTEND (Next.js)
  # ═══════════════════════════════════════
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        NEXT_PUBLIC_API_URL: https://api.pdimaker.com.br
    container_name: pdimaker-web
    restart: always
    environment:
      NODE_ENV: production
      NEXTAUTH_URL: https://pdimaker.com.br
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET}
      DATABASE_URL: postgresql://postgres:${DB_PASSWORD}@postgres:5432/pdimaker_prod
    volumes:
      - ./frontend:/app
      - /app/node_modules
      - /app/.next
    ports:
      - "3300:3000"
    depends_on:
      - backend
      - postgres
    networks:
      - pdimaker-network

  # ═══════════════════════════════════════
  # NGINX (Reverse Proxy + SSL)
  # ═══════════════════════════════════════
  nginx:
    image: nginx:alpine
    container_name: pdimaker-nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./ssl:/etc/nginx/ssl:ro
      - /var/log/nginx:/var/log/nginx
    depends_on:
      - frontend
      - backend
    networks:
      - pdimaker-network

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

networks:
  pdimaker-network:
    driver: bridge
