version: '3.8'

services:
  frontend:
    build:
      context: ./fe
      dockerfile: Dockerfile
      target: runner
    container_name: iddaai-fe
    restart: unless-stopped
    ports:
      - '127.0.0.1:1510:3000'
    environment:
      - NODE_ENV=production
      - NEXT_PUBLIC_API_URL=https://api.iddaai.com/api
      - NEXTAUTH_URL=https://iddaai.com
      - NEXTAUTH_SECRET=fFw34R134jRof1H2jofh2!32hU3gfjA1
      - NEXT_PUBLIC_AUTH_REQUIRED=false
    networks:
      - iddaai-network

  backend:
    build:
      context: ./be
      dockerfile: Dockerfile
      target: production
    container_name: iddaai-be
    restart: unless-stopped
    ports:
      - '127.0.0.1:1810:3000'
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgresql://iddaai_user:IddaA1_S4crET!@postgres:5432/iddaai_db?schema=public
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - REDIS_PASSWORD=IddaA1_Redis_Pass!
      - AI_ENGINE_URL=http://ai-engine:8000
      - JWT_SECRET=b7V8jM2wP1L5mQxs2RdfFkAsLpI2oG!w
      - JWT_ACCESS_EXPIRATION=1d
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    # Start up the backend: run Prisma migrations then start the server
    command: /bin/sh -c "npx prisma migrate deploy && node dist/main.js"
    networks:
      - iddaai-network

  ai-engine:
    build:
      context: ./be/ai-engine
      dockerfile: Dockerfile
    container_name: iddaai-ai-engine
    restart: unless-stopped
    environment:
      - DATABASE_URL=postgresql://iddaai_user:IddaA1_S4crET!@postgres:5432/iddaai_db?schema=public
    depends_on:
      postgres:
        condition: service_healthy
    command: uvicorn main:app --host 0.0.0.0 --port 8000
    networks:
      - iddaai-network

  postgres:
    image: postgres:17-alpine
    container_name: iddaai-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_USER=iddaai_user
      - POSTGRES_PASSWORD=IddaA1_S4crET!
      - POSTGRES_DB=iddaai_db
    volumes:
      - iddaai_db_data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U iddaai_user -d iddaai_db']
      interval: 10s
      timeout: 10s
      retries: 10
      start_period: 30s
    networks:
      - iddaai-network

  redis:
    image: redis:7-alpine
    container_name: iddaai-redis
    restart: unless-stopped
    command: redis-server --requirepass IddaA1_Redis_Pass!
    volumes:
      - iddaai_redis_data:/data
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - iddaai-network

networks:
  iddaai-network:
    driver: bridge

volumes:
  iddaai_db_data:
  iddaai_redis_data:
