version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: restaurant-postgres
    environment:
      POSTGRES_USER: ${DB_USER:-restaurant_user}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-restaurant_pass}
      POSTGRES_DB: ${DB_NAME:-restaurant_db}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "${DB_PORT:-5432}:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-restaurant_user}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - restaurant-network

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: restaurant-redis
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
    networks:
      - restaurant-network

  # Backend API
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: restaurant-backend
    environment:
      NODE_ENV: production
      PORT: 5000
      DATABASE_URL: postgresql://${DB_USER:-restaurant_user}:${DB_PASSWORD:-restaurant_pass}@postgres:5432/${DB_NAME:-restaurant_db}
      REDIS_URL: redis://redis:6379
      JWT_SECRET: ${JWT_SECRET}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET}
      JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-7d}
      JWT_REFRESH_EXPIRES_IN: ${JWT_REFRESH_EXPIRES_IN:-30d}
      ADMIN_URL: ${ADMIN_URL:-http://localhost}
      CUSTOMER_URL: ${CUSTOMER_URL:-http://localhost}
      FRONTEND_URL: ${CUSTOMER_URL:-http://localhost}
      PAYSTACK_SECRET_KEY: ${PAYSTACK_SECRET_KEY:-}
      PAYSTACK_PUBLIC_KEY: ${PAYSTACK_PUBLIC_KEY:-}
    ports:
      - "${BACKEND_PORT:-5000}:5000"
    volumes:
      - ./backend/uploads:/app/uploads
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - restaurant-network
    restart: unless-stopped
    command: sh -c "npx prisma migrate deploy && npm start"

  # Nginx Reverse Proxy
  nginx:
    image: nginx:alpine
    container_name: restaurant-nginx
    ports:
      - "${HTTP_PORT:-80}:80"
      - "${HTTPS_PORT:-443}:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./frontend-admin/dist:/usr/share/nginx/html/admin:ro
      - ./frontend-customer/dist:/usr/share/nginx/html/customer:ro
      - ./backend/uploads:/usr/share/nginx/html/uploads:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
    depends_on:
      - backend
    networks:
      - restaurant-network
    restart: unless-stopped

volumes:
  postgres_data:
  redis_data:

networks:
  restaurant-network:
    driver: bridge

