services:
  # PostgreSQL Database
  postgres:
    image: postgres:16-alpine
    container_name: chat-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${DB_USER:-chat}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-chatpassword}
      POSTGRES_DB: ${DB_NAME:-chat}
    ports:
      - "${DB_PORT:-5433}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./init-db:/docker-entrypoint-initdb.d
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-chat} -d ${DB_NAME:-chat}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Chat Backend API
  backend:
    build:
      context: ../..
      dockerfile: chat/backend/Dockerfile
    container_name: chat-backend
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      # Database
      DATABASE_URL: postgresql://${DB_USER:-chat}:${DB_PASSWORD:-chatpassword}@postgres:5432/${DB_NAME:-chat}
      DB_HOST: postgres
      DB_PORT: 5432
      DB_USER: ${DB_USER:-chat}
      DB_PASSWORD: ${DB_PASSWORD:-chatpassword}
      DB_NAME: ${DB_NAME:-chat}

      # Azure OpenAI
      AZURE_OPENAI_ENDPOINT: ${AZURE_OPENAI_ENDPOINT}
      AZURE_OPENAI_API_KEY: ${AZURE_OPENAI_API_KEY}
      AZURE_OPENAI_API_VERSION: ${AZURE_OPENAI_API_VERSION:-2024-12-01-preview}

      # Mana Core Auth
      MANA_CORE_AUTH_URL: ${MANA_CORE_AUTH_URL:-http://host.docker.internal:3001}

      # Server
      PORT: 3002
      NODE_ENV: production
    ports:
      - "${BACKEND_PORT:-3002}:3002"
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3002/api/health"]
      interval: 30s
      timeout: 10s
      start_period: 30s
      retries: 3

volumes:
  postgres_data:
    driver: local

networks:
  default:
    name: chat-network
