services:
  postgres:
    image: postgres:16-alpine
    container_name: tudocerto-postgres
    environment:
      POSTGRES_DB: tudocerto
      POSTGRES_USER: tudocerto
      POSTGRES_PASSWORD: tudocerto123
    ports:
      - "5434:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U tudocerto -d tudocerto"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    container_name: tudocerto-redis
    ports:
      - "6381:6379"

  backend:
    build: ./backend
    container_name: tudocerto-backend
    environment:
      DATABASE_URL: postgresql://tudocerto:tudocerto123@postgres:5432/tudocerto?schema=public
      JWT_SECRET: tudocerto-dev-secret-change-me
      OCR_SERVICE_URL: http://ocr-service:8000
      REDIS_URL: redis://redis:6379
      GS1_API_URL: ${GS1_API_URL:-}
      GS1_API_KEY: ${GS1_API_KEY:-}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
      ocr-service:
        condition: service_started
    ports:
      - "4000:4000"
    command: sh -c "npx prisma db push && npm run seed && npm run start:prod"

  ocr-service:
    build: ./ocr-service
    container_name: tudocerto-ocr
    ports:
      - "8000:8000"
    healthcheck:
        test: ["CMD", "curl", "-f", "http://b2jdev.com.br:8000/health"]
        interval: 15s
        timeout: 10s
        retries: 20

  frontend:
    build: ./frontend
    container_name: tudocerto-frontend
    environment:
      NEXT_PUBLIC_API_URL: http://b2jdev.com.br:4000
    depends_on:
      - backend
    ports:
      - "3000:3000"

  nginx:
    image: nginx:1.27-alpine
    container_name: tudocerto-nginx
    depends_on:
      - frontend
      - backend
      - ocr-service
    ports:
      - "8088:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro

volumes:
  postgres_data:
