version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: pet360-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: pet360
      POSTGRES_PASSWORD: pet360_secret
      POSTGRES_DB: pet360
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U pet360"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: pet360-redis
    restart: unless-stopped
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Backend API
  api:
    build:
      context: .
      dockerfile: apps/api/Dockerfile
    container_name: pet360-api
    restart: unless-stopped
    environment:
      NODE_ENV: production
      DATABASE_URL: postgresql://pet360:pet360_secret@postgres:5432/pet360
      REDIS_URL: redis://redis:6379
      FRONTEND_URL: ${FRONTEND_URL:-http://pet360.inema.online}
      JWT_SECRET: ${JWT_SECRET:-your-super-secret-jwt-key-change-in-production}
      JWT_EXPIRATION: 15m
      JWT_REFRESH_EXPIRATION: 7d
      EVOLUTION_API_URL: ${EVOLUTION_API_URL:-http://evolution:8080}
      EVOLUTION_API_KEY: ${EVOLUTION_API_KEY:-your-evolution-api-key}
    ports:
      - "3001:3001"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:3001/api/docs"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Frontend Web
  web:
    build:
      context: .
      dockerfile: apps/web/Dockerfile
      args:
        # IMPORTANT: Set to your public domain URL for production
        NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-http://pet360.inema.online}
    container_name: pet360-web
    restart: unless-stopped
    environment:
      NODE_ENV: production
    ports:
      - "3000:3000"
    depends_on:
      - api

  # Evolution API (WhatsApp)
  evolution:
    image: atendai/evolution-api:latest
    container_name: pet360-evolution
    restart: unless-stopped
    environment:
      SERVER_URL: ${EVOLUTION_SERVER_URL:-http://localhost:8080}
      AUTHENTICATION_TYPE: apikey
      AUTHENTICATION_API_KEY: ${EVOLUTION_API_KEY:-your-evolution-api-key}
      DATABASE_ENABLED: true
      DATABASE_PROVIDER: postgresql
      DATABASE_CONNECTION_URI: postgresql://pet360:pet360_secret@postgres:5432/pet360
    ports:
      - "8080:8080"
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - evolution_instances:/evolution/instances
      - evolution_store:/evolution/store

  # Nginx Reverse Proxy (optional - for production)
  nginx:
    image: nginx:alpine
    container_name: pet360-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./ssl:/etc/nginx/ssl:ro
    depends_on:
      - web
      - api

volumes:
  postgres_data:
  redis_data:
  evolution_instances:
  evolution_store:

networks:
  default:
    name: pet360-network
